Page 1 of 1

[wxJSON] building errors on some platforms?

Posted: Thu May 14, 2009 4:30 pm
by luccat
Hi all,
I start this topic in response to a bug report (see ID=2735592).
wxJSON did not compile on mingw, gcc4; at first glance, it seems the problem related to wxWidgets 2.8.10 but it is not. The library compiles fine on my linux machine and on windows using BCC 5.5.
I thought the problem may be related to wxHashMap and its macros so I decided to implement a new feature in wxJSON: by default, the library uses the wxWidget's own implementation of container classes but the user may specify a flag to force the library using STL containers: sdt::vector and std::map.

I wrote the code and got a pletora of compiler's errors. It seems the problem is related to STL which do not like forward declations.
In order to keep the problem simple, I wrote a code fragment that illustrate the error:

Code: Select all

#include <vector>

class MyClass
{
public:
	int	iData;
	MyClass( int i = 0)
	{
		iData = i;
	}

	int   GetInt() const
	{
		return iData;
	}
};

typedef std::vector<MyClass>	MyClassArray;
The above code compiles fine on my linux machine. But wxJSONValue class can contain arrays of itself so the class's declaration is much like the following:

Code: Select all

#include <vector>

// forward declaration needed
class MyClass2Array;

class MyClass2
{
public:
	int	iData;
	MyClass2( int i = 0)
	{
		iData = i;
	}

	int   GetInt() const
	{
		return iData;
	}

	MyClass2Array*	array;  // the array
};
typedef std::vector<MyClass2>	MyClass2Array;

The above code does not compile. The compiler GCC 4.3.0 says that the class MyClass2Array was previously defined as 'struct MyClass2Array' although this is not true,
Here the compiler output:

Code: Select all

samples/teststd.cpp:74: error: conflicting declaration ‘typedef class std::vector<MyClass2, std::allocator<MyClass2> > MyClass2Array’
samples/teststd.cpp:57: error: ‘struct MyClass2Array’ has a previous declaration as ‘struct MyClass2Array’
samples/teststd.cpp: In function ‘int main(int, char**)’:
samples/teststd.cpp:84: error: aggregate ‘MyClass2Array array2’ has incomplete type and cannot be defined
samples/teststd.cpp:84: warning: unused variable ‘array2’
make: *** [teststd.o] Error 1
What is wrong with my code?
Is your compiler able to compile the code snippset (you find it in the attachment)?
Luciano

[wxJSON] building errors on some platforms?

Posted: Fri May 15, 2009 4:12 pm
by luccat
Hi,
the code snippset does not compile even in Borland BCC 5.5.

Here is the compiler output

Code: Select all

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
teststd.cpp:
Error E2238 teststd.cpp 74: Multiple declaration for 'MyClass2Array'
The offending line is the std::vector declaration.

The code is surely wrong!
Luciano