tags:

views:

171

answers:

3

Been awhile since I've used structs in C++.

Any idea why this isn't working? My compiler is complaining about DataStruct not being a recognized type but Intellisense in VC++ is still able to see the data members inside the struct so the syntax is ok...

Frustating. xD

struct DataStruct
{
    int first;
};



int main(int argc, char **argv)
{   
    DataStruct test;
    //test.first = 1;
}
+1  A: 

You need to use struct DataStruct to refer to the struct.

Alternatively, you can typedef it as DataStruct if don't want to use the "struct" everywhere.

Anon.
Many thanks! Not sure why I didn't remember this from previous classes.... xD
bobber205
Not in C+ you don't.
anon
This is why instantly accepting the first answer that fixes the problem isn't really a good idea. Often, it misses the point entirely.
Anon.
This simply isn't true. As other people have written, the most likely problem is you've saved your file as a ".c" or are in some other way compiling as C instead of C++.
The thing is that even if you don't *need* to do it in C++, it doesn't hurt.
Michael Burr
+16  A: 

Are you sure you are compiling the file as C++? If you compile it as C (i.e. if the file has a .c rather than a .cpp extension), you will have problems.

anon
That was it. Was using a template and didn't notice it was .c and not .cpp
bobber205
This is my first guess as well. Above code is legit C++ but not legit C.
Just to clarify : Bobber is talking about VC++ IDE templates and not `template` s in C++.
missingfaktor
Another reason to as a rule typedef structs - then you don't have to worry about whether it'll compile as C or C++ - it'll work in both. Of course, it's always a pretty good idea to know if you're working C or C++, but why not make code work in both if the only downside is a bit more typing (pun intended) that you can probably get your editor to do for you?
Michael Burr
@Michael When I'm writing C++ code, I never expect it to be compilable under C. For example, I normally give structs a constructor. So using a typedef would be pointless.
anon
@Neil: understood. I write enough C code (embedded stuff) that 'typedef'ing structs just happens.
Michael Burr
+4  A: 

You are compiling as C code. C requires you to refer to it using the "Struct" keyword or typedef it. C++ does not.

iconiK