tags:

views:

153

answers:

3

Hi i have to port some stuff written on c++ from unix bases os to windows visual studio 2008. The following code implements array data type with void ** - pointer to the data.


struct array
{
    int id;
    void **array; // store the actual data of the array
    // more members
}

When i compile with g++ on Unix it's ok but when i try with MSVS 2008 I get the error - error C2461: 'array' : constructor syntax missing formal parameters. When i change the member from 'array' to something else it works, so it seems that the compiler thinks that the member name 'array' is actually the constructor of the struct array. It's obviously not a good practice to name the member like the struct but it's already written that way. Can i tell the MSVS compiler to ignore this problem or i should rename all members that are the same as the struct name.

+3  A: 

I'd say that if you're doing something that you yourself describe as "not a good practice", then you should change it.

Graeme Perrow
the problem is that the c c++ code that is written for unix is not written from me my job is just to port it on windows :)
Mojo Risin
+2  A: 

You are dealing with a bug in GCC compiler. C++ language explicitly prohibits having data members whose name is the same as the name of the class (see 9.2/13). MS compiler is right to complain about it. Moreover, any C++ compiler is required to issue a diagnostic message in this case. Since GCC is silent even in '-ansi -pedantic -Wall' mode, it is a clear bug in GCC.

Revison: What I said above is only correct within the "classic" C++98 specification of C++ language. In the most recent specification this requirement only applies to static data members of the class. Non-static data members can now share the name with the class. I don't know whether this change is already in the official version of the revised standard though.

That means that both compilers are correct in their own way. MS compiler sticks to the "classic" C++98 specification of the language, while GCC seems to implement a more recent one.

AndreyT
If that's the case, then he should be able to compile with MinGW's compiler, as it's a port of GCC. I would expect it to have the same behaviors (including buggy behaviors) as GCC.
Thomas Owens
It is in C++ 2003 (section 9.2/13). Paragraph 13a adds a requirement that if the class contains a user-declared ctor, non-static data members must have a different name from the class name as well.
Jerry Coffin
+2  A: 

I would rename your attribute to not have the same name as the class. This will make your code more portable. If you have to move to yet another compiler in the future, you won't run in to this problem again then.

Zanson