Porting C ++ code from unix to windows

Hi, I need to migrate some stuff written in C ++ from unix os databases to windows visual studio 2008. The following code implements the array data type with void ** - pointer to data.


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

      

When I compile with g ++ on Unix this is fine, but when I try with MSVS 2008 I get an error - error C2461: "array": the constructor syntax has no formal parameters. When I change the member from "array" to something else, it works, so it seems like the compiler thinks that the item name "array" is actually the constructor of the struct. Obviously it's not a good practice to name a member as a structure, but it's already written that way. Can I tell the MSVS compiler to ignore this issue, or should I rename all members that match the name of the struct.

+2


source to share


3 answers


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

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



This means that both compilers are correct in their own way. The MS compiler adheres to the "classic" C ++ 98 language specification, while GCC seems to be using a newer version.

+5


source


I would say that if you are doing what you yourself describe as "not good practice," then you have to change it.



+3


source


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

+2


source







All Articles