Jump to content
  • Advertisement
Sign in to follow this  
Drunken_Monkey

Totally Weird Error(C++)

This topic is 4502 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

#include "iostream" #include "cstdlib" #include "ctime" #include "class basePlayer.h" using namespace std; int main() { time_t t; srand((unsigned) time(&t)); return 0; } ////////////////////////////////////////////////////////////////////// //class basePlayer.h file #ifndef basePlayer #define basePlayer #include <vector> #include <string> using namespace std; class basePlayer { public: //Constructors basePlayer();//error C2059: syntax error : ')' error C2238: unexpected token(s) preceding ';' //Pointers basePlayer* enemy;//error C2501: 'enemy' : missing storage-class or type specifiers //Stats int HP, MP, Strength, Defence, Luck, Speed, totalExperience, experience, Level;//Basic stats string Name; };//error C2639: compiler generated default constructor required by unnamed class This is compiled with MVC 6.0. I posted the errors next to the lines, and they make no sense. All the code is correct... right?

Share this post


Link to post
Share on other sites
Advertisement
Why have you got:

#include "class baseplayer.h"

take out the class and the space and recompile.

Dave

Share this post


Link to post
Share on other sites
Looks fine to me. Sounds like another famout MSVC 6 problem. I'd highly recommed upgrading to VS2005 if possible. The express version is even free.

Have you tried doing Build -> Clean, and recompiling?

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
Why have you got:

#include "class baseplayer.h"

take out the class and the space and recompile.

Dave


Thats the name of his file. Its non conventional, but he will still have to rename the header file too if that is the problem.

Share this post


Link to post
Share on other sites
Usually the defines are the name of the header file:

#ifndef _basePlayer_h_
#define _basePlayer_h_

<your class code>...

#endif <---- is this included at the end in your code?

Also, is there a body for the constructor somewhere?

//Constructors
basePlayer() { }

Share this post


Link to post
Share on other sites
all these #ifndef .. #define .. #endif constructions are very popular, i know; but insted of doing this, you can do also...

#pragma once

...in your headerfiles.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alex Baker
all these #ifndef .. #define .. #endif constructions are very popular, i know; but insted of doing this, you can do also...

#pragma once

...in your headerfiles.


Or you could use a method that will work under any standards compliant compiler, the usual include guards...

Share this post


Link to post
Share on other sites
Quote:
Original post by Alex Baker
all these #ifndef .. #define .. #endif constructions are very popular, i know; but insted of doing this, you can do also...

#pragma once

...in your headerfiles.


That only works for MSVC. If you want your code to compile with other compilers, you should use the normal include guards.

Share this post


Link to post
Share on other sites
As was mentioned before, you #define basePlayer and then try to use the same name for your class. Basically what is happening is this (at least I'm fairly certain):

1.) You define an empty constant named basePlayer. #define instructs the compiler to replace any instance of the name you defined with the value you specify after it. In this case, you're instructing the preprocessor (runs before the compiler) to replace every instance of the name "basePlayer" with "" (nothing).

2.) You try to name a class basePlayer. The preprocessor sees this string and replaces it with any empty string.

3.) When you try to declare the constructor, the compiler actually sees this:
public: ();
and properly notifies you that this makes no sense.

4.) At the pointer declaration, the compiler sees this:
* enemy;
and properly notifies you that there is no "storage-class or type specifier" preceding the name enemy, which it requires.

5.) And finally, the real clue to your problem is the last error, where it's complaining about the unnamed class. Again, it's because you've instructed the preprocessor to treat that name as an empty string.

Compiler error messages can often be very cryptic and misleading, but in this case it turns out they were pretty much straight to the point.

As was also mentioned above, and as is advisable any time you're #defining things in the preprocessor, you should use names that won't appear elsewhere in your code. Often people use all capital letters for such things. The common standard for preventing multiple header inclusion is:

#ifdef MYCLASSNAME_H
#define MYCLASSNAME_H

#endif

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!