Page 1 of 1

WX_DECLARE/DEFINE_OBJARRAY fails to link

Posted: Wed Apr 30, 2008 3:30 pm
by Blinkinhek
I get a link error
"Multiple definition of <objarrayname> ... first defined here ..."

whenever I use the Object Arrays:
eg
#include <wx/dynarray.h>
#include <wx/arrimpl.cpp>
class ctest
{
public:
int m_data;

void DoStuff(<params>) {stuff;}
};

Any thoughts on what I am doing wrong?

WX_DECLARE_OBJARRAY(ctest, ArrayOfTest);
WX_DEFINE_OBJARRAY(ArrayOfTest);

Posted: Wed Apr 30, 2008 3:39 pm
by lester
header:

#include <wx/dynarray.h> // we must forward declare the array because it is used inside the class
class MyDirectory;
// now used as shown below
WX_DECLARE_OBJARRAY(MyDirectory, ArrayOfDirectories);



.cpp file:
#include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
WX_DEFINE_OBJARRAY(ArrayOfDirectories);


So You must move part of code from header file to .cpp and use something like:

#ifndef __MYHEADER__
#define __MYHEADER__

class...
..

#endif // __MYHEADER__

for .h file

Posted: Wed Apr 30, 2008 4:09 pm
by Blinkinhek
lester wrote:header:

#include <wx/dynarray.h> // we must forward declare the array because it is used inside the class
class MyDirectory;
// now used as shown below
WX_DECLARE_OBJARRAY(MyDirectory, ArrayOfDirectories);



.cpp file:
#include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
WX_DEFINE_OBJARRAY(ArrayOfDirectories);


So You must move part of code from header file to .cpp and use something like:

#ifndef __MYHEADER__
#define __MYHEADER__

class...
..

#endif // __MYHEADER__

for .h file
wow - that was quick !
Simply moving the WX_DEFINE_OBJARRAY to the .cpp file cured it.
It was originally in the header, and errored even the the header did have wrappers of #ifndef _MYHEADER_ ...#endif as you had suggested.
Anyway many thanks! :)