Skip to main content
gamedev.net
🔒 Locked

Quick question

Started by BioSquirrel 18 replies 550+ views
Original Post
BioSquirrel
BioSquirrel
OK everything was working fine, then I compile my program and it says that when I did this: Player Player1; I got this error: rror C2146: syntax error : missing '';'' before identifier ''Player1'' C:\unzipped\lesson4[1]\CZServer\CZServer.cpp(47) : error C2501: ''Player'' : missing storage-class or type specifiers C:\unzipped\lesson4[1]\CZServer\CZServer.cpp(47) : error C2086: ''Player'' : redefinition ------------------ Stupid Morons- my current projects
BioSquirrel
BioSquirrel
It was just working and I cant find a typo...heres the whole thing:

class Player {
public:
int flags;
int xpos;
int ypos;
int lazerxpos;
int lazerypos;
int lazerdx;
int lazerdy;
int fire;
int dead;
int deadxpos;
int deadypos;
int grenadexpos;
int grenadeypos;
int grenadedx;
int grenadedy;
int explosionxpos;
int explosionypos;
int fire2;
int money;
char name[20];
};

Player Player1;
Player Player2;

------------------
Stupid Morons- my current projects
aNonamuss
aNonamuss
Where is the source file? You are declaring Player1 and Player2 in the header file?






------

If I''m incorrect, sue me, you''ll get nothing
BioSquirrel
BioSquirrel
Any higher and I''m in the header files, which didn''t do anything (I tried).

------------------
Stupid Morons- my current projects
aNonamuss
aNonamuss
Why are you declaring a class in a .cpp file? You should be doing that in a .h file. Your implementation should be in a .cpp file.



    
// whatever.h

// header file

#ifndef WHATEVER_H
#define WHATEVER_H

class Player {
public:
int flags;
int xpos;
int ypos;
int lazerxpos;
int lazerypos;
int lazerdx;
int lazerdy;
int fire;
int dead;
int deadxpos;
int deadypos;
int grenadexpos;
int grenadeypos;
int grenadedx;
int grenadedy;
int explosionxpos;
int explosionypos;
int fire2;
int money;
char name[20];
};

#endif




        
// whatever.cpp

// source file

#include whatever.h

// what ever main you are using win32 or main(command prompt)

// will use main(command prompt) for simplicity

void main(void)
{
Player Player1;
Player Player2;
}





------

If I'm incorrect, sue me, you'll get nothing

[edited by - aNonamuss on April 12, 2002 10:22:44 PM]
Oluseyi
Oluseyi
quote:
Original post by aNonamuss
Why are you declaring a class in a .cpp file? You should be doing that in a .h file. Your implementation should be in a .cpp file.

Oh, shut up! Your class declaration should be wherever it is visible to all the code that is dependent on it.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
aNonamuss
aNonamuss
Hey, I'm just telling him the object-oriented way. So shut yourself up! There is a reason that header files exist.

If you ever want to program in a business environment, you mind as well conform to the norm.





------

If I'm incorrect, sue me, you'll get nothing

[edited by - aNonamuss on April 12, 2002 12:01:05 AM]
Oluseyi
Oluseyi
quote:
Original post by aNonamuss
Hey, I''m just telling him the object-oriented way. So shut yourself up! There is a reason that header files exist.

Yeah, and that''s not it.

If a class is an internal implementation detail that will not be used outside of the classes/functions that require it to function, and if it is only needed at implementation (ie, not at declaration), why stick it in a header? Just to have more files to manage?

This is what happens when people learn language by rote. You apparently lack a deeper understanding of the purpose for these constructs (classes, header files and implementation files). I suppose you''d tell me that to globally declare a utility function in a header "utility.h" I''d also need a source file for the implementation - which would mean the header would contain just one line.

Hint: static.

quote:

If you ever want to program in a business environment, you mind as well conform to the norm.

Been there, done that. Any other "gems" of misinformation?

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
aNonamuss
aNonamuss
quote:

If a class is an internal implementation detail that will not be used outside of the classes/functions that require it to function, and if it is only needed at implementation (ie, not at declaration), why stick it in a header? Just to have more files to manage?



Well, I guess you can say that about all classes then. Why even have header files? Why don''t we just get rid of them altogether, according to your theory! Why you just put all of your source code and declartion in one file, gee, then you only have one file to manage.
Oluseyi
Oluseyi
quote:

I said:
If a class is an internal implementation detail that will not be used outside of the classes/functions that require it to function, and if it is only needed at implementation (ie, not at declaration), why stick it in a header? Just to have more files to manage?

Then aNonamuss said:
Well, I guess you can say that about all classes then. Why even have header files? Why don't we just get rid of them altogether, according to your theory! Why you just put all of your source code and declartion in one file, gee, then you only have one file to manage.

Your belligerence borders on stupidity.

Header and source files are a means, not an end, which means you should use them as is logical for the structure of your project. Don't feel that you need to have a header and a source file for each class or any such nonsense. If I have a class A which is the only class in the entire project that uses or will ever use class B, and class B is absolutely project-specific, and class B is only needed as a runtime structure in certain methods in A, why would I stick B into a header that will only be included in A.cpp? Why not just declare B at the top of A.cpp?

Don't bother answering that. You can't.

What I'm objecting to is the connotation that BioSquirrel did something "wrong" by having his class declaration in the source file - like that could be the source of his compile errors. Both headers and sources are plain text files; the only difference is how your particular development environment interprets them. You essentially infer that if I was writing a small, one-file utility I'd need to have a header just to include my structures?

That is so bone-headed.

[Edit: HTML.]

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!


[edited by - Oluseyi on April 13, 2002 12:58:52 PM]
aNonamuss
aNonamuss
Calm down! You made your point, I''ll agree with you since you put it that way. When it''s a small project, sure, go ahead and do what you want with your file structure.

But I''m just saying that if you get into a big project, it is safe to use header files for all your public classes. Besides, where do you think Valve software would be if they didn''t have header files. I guess people wouldn''t be making mods, you can''t make a mod working off object files unless you know what functions you have access to.

If a class is private to another class, then go ahead and stick it in a source .cpp file. But if BioSquirrel does that then I guess he doesn''t plan on making the game portable unless his Player class is private.
Spiral
Spiral
quote:
Original post by aNonamuss
But I''m just saying that if you get into a big project, it is safe to use header files for all your public classes. Besides, where do you think Valve software would be if they didn''t have header files. I guess people wouldn''t be making mods, you can''t make a mod working off object files unless you know what functions you have access to.



You''ve completely missed the point of what Oluseyi is trying to explain. Re-read his posts.
--------------------Go Stick Your Head In A Pig
BioSquirrel
BioSquirrel
Well, it did work to put them in a header file but I''ve never had to do that before...

------------------
Stupid Morons- my current projects
Xai
Xai
Are you #includeing that header file in any OTHER files ... that''s the only case where there is a different, if the class is used in MULTIPLE places ...

IF you have defined the class in your .cpp, AND #included the .cpp (with the two instances) in another file ... then you''d get a redefinition error ...

DID YOU try the force build whole project thing first? In Visual C++ it is often simply a confused build process ... anytime you get a REALLY strage error, rebuild all first ...

ALSO .. .there is one last kind of error that is really crazy, occastionally there will be an invisible chracter (non-printable) in your file (like when you cut and paste something off an HTML page sometimes)... and you can''t see it, but you''ll get a stupid parse error ... in that case, when you''ve exhausted all other posiblities ... simply delete the top section of your file right above the error, usually 2-3 lines worth .. and retype it by hand ... I''ve had this happen twice in the last year ... and it SUCKS to track down.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.