• Advertisement
Sign in to follow this  

Does MS C++ Express support the keyword ' struct'?

This topic is 3328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Does MS C++ Express support the keyword ' struct'? I ask because I used to code with VS 6 ," a long time ago - I know!" and now I've tried it in MS C++ Express Edition the same way as before and it keeps giving me errors when I try to compile! Does anyone know of this problem off hand before I post my code ?

Share this post


Link to post
Share on other sites
Advertisement
Well, there's no problem with struct. If you can from VC++6, try #defining

#define for if(1) for

then re-compile (in vc6). If you now get the same errors, the problem is for loop scoping...


for(int i=0;i<10;++i)
{
}

// this will cause an error on VC7+, but is valid in VC6.
std::cout << i <<std::endl;


Share this post


Link to post
Share on other sites

struct SHAPE
{
int x;
int y;
int rotation;
int x_size;
int y_size;
};

SHAPE shapeA;

void main()
{
shapeA.rotation=4;
}



main.cpp(9) : error C2065: 'shapeA' : undeclared identifier
main.cpp(9) : error C2228: left of '.rotation' must have class/struct/union
type is ''unknown-type''

I MUST be doing something wrong : this should be straight forward but it appears to be extremely frustrating!!!

Help!

Share this post


Link to post
Share on other sites
Does your project define, by any chance, the preprocessor definition "SHAPE" ? Other than that, I cannot imagine what could cause your code to fail.

Share this post


Link to post
Share on other sites
No, the code I posted above is the ONLY code I am using at the moment. If I change the 'shapeA' to any other variable name I still get the same errors!

Share this post


Link to post
Share on other sites
Heres a thought - when you try to access the member variable of a structure like
 shapeA.rotation 
aren't you supposed to get a drop down list of the available members when you type the dot "."?

I think I am but I aint gettin it.

Share this post


Link to post
Share on other sites
Works fine for me. Are you sure this is the only code you have? Have you tried starting a new, clean project from scratch and pasting the code into there?

Share this post


Link to post
Share on other sites
What if you try to change the name of the struct type to something else than SHAPE?

Share this post


Link to post
Share on other sites
Hi guys thanks for your help. I have implemented both of your ideas and did start a new project from scratch and renamed the struct SHAPE to TETRIS_SHAPE as I am trying to code a clone.

Still doesn't compile --- here are the errors now.

Linking...
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\Documents and Settings\Chris\My Documents\Programming\C++\Examples\Struct_2\Debug\Struct_2.exe : fatal error LNK1120: 1 unresolved externals

Share this post


Link to post
Share on other sites
You need to leave your program as a Win32 app and implement WinMain or make it a console app and continue using main. The entry-point for a Win32 application is WinMain (something like: int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)).

Share this post


Link to post
Share on other sites
That's easier. You need to make a new "win32 CONSOLE" project if you want to use "int main()".

Otherwise you need to look up "winmain" and the windows message pump.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
That's easier. You need to make a new "win32 CONSOLE" project if you want to use "int main()".


Yep. If, however, you want to use "void main()", you will have to write C, because that is not valid in C++. ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by styfir
Hi guys thanks for your help. I have implemented both of your ideas and did start a new project from scratch and renamed the struct SHAPE to TETRIS_SHAPE as I am trying to code a clone.

Still doesn't compile --- here are the errors now.

Linking...
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\Documents and Settings\Chris\My Documents\Programming\C++\Examples\Struct_2\Debug\Struct_2.exe : fatal error LNK1120: 1 unresolved externals


That means it compiles, what it doesn't do is link.

As already been said, switch your project options to console application.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
If, however, you want to use "void main()", you will have to write C, because that is not valid in C++. ;)

It's not valid C either.

Share this post


Link to post
Share on other sites
Zahlman, I don't understand what you mean by:

"If, however, you want to use "void main()", you will have to write C, because that is not valid in C++. ;)"

I have used "void main()" in C++ code since...forever! It's the standard. The only time you use something different is when you write windows code (don't know of anything else...) where you use WinMain.

Share this post


Link to post
Share on other sites
Incidentally, I think you'd get the original error if the extension on the file you created was .c instead of .cpp. I can't think of many other reasons.

Share this post


Link to post
Share on other sites
Quote:
Original post by osmanb
Incidentally, I think you'd get the original error if the extension on the file you created was .c instead of .cpp. I can't think of many other reasons.


IIRC ".C" is a valid file extension for a cpp source file and seeing Windows is not case sensitive then ".c" should also be valid.

Share this post


Link to post
Share on other sites
Quote:
Original post by CmpDev
Quote:
Original post by osmanb
Incidentally, I think you'd get the original error if the extension on the file you created was .c instead of .cpp. I can't think of many other reasons.


IIRC ".C" is a valid file extension for a cpp source file and seeing Windows is not case sensitive then ".c" should also be valid.
A file with extension .c will be treated as C code rather than C++ by Visual Studio, and if that was the case then the struct would need to be typedef'd or shapeA's declaration would have to be:
struct SHAPE shapeA;
Which woukd probably give the error indicated. Although the error message states "main.cpp" so that may not be the problem...

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by CmpDev
Quote:
Original post by osmanb
Incidentally, I think you'd get the original error if the extension on the file you created was .c instead of .cpp. I can't think of many other reasons.


IIRC ".C" is a valid file extension for a cpp source file and seeing Windows is not case sensitive then ".c" should also be valid.
A file with extension .c will be treated as C code rather than C++ by Visual Studio, and if that was the case then the struct would need to be typedef'd or shapeA's declaration would have to be:
struct SHAPE shapeA;
Which woukd probably give the error indicated. Although the error message states "main.cpp" so that may not be the problem...


Are you sure? So an OS which is not case sensitive by default is when using source files?

Share this post


Link to post
Share on other sites
Edit: I was wrong, indeed for C++ you can live without the struct keyword. Original reply below:

Your line

SHAPE shapeA;


is wrong. It should be


struct SHAPE shapeA;


I think using struct keyword may have been sort of optional in older C compilers, but it is not really optional. Unless you make some sort of typedef, but personally, I wouldn't.

[Edited by - tksuoran on January 13, 2009 7:28:55 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by tksuoran
Your line

SHAPE shapeA;


is wrong. It should be


struct SHAPE shapeA;


I think using struct keyword may have been sort of optional in older C compilers, but it is not really optional. Unless you make some sort of typedef, but personally, I wouldn't.

This would be correct if the title did not state that this is C++ code not C.
EvilSteve as I thought Msdn lists ".c" as a valid extension for c++ projects

Share this post


Link to post
Share on other sites
C++ injects the struct namespace into the global namespace, so no need for the extra struct if we're talking C++.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement