Jump to content
  • Advertisement
Sign in to follow this  
zChrisse

Include erro, atleast thats what i think

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

I get some "strange" errors when i include CPlayer.h in main.cpp, but when i compile the CPlayer.h separatly with "Compile currently file" it compiles just fine. It´s only when i inlcude it in main.cpp and try to compile the whole project or just the main file i get these errors :/ Here´s the CPlayer.h code
#ifndef CPlayer
#define CPlayer

#include <exception>
#include <string>

class CPlayer
{
      public:
             CPlayer();
             int getYpos() { return y; }
             int getXpos() { return x; }
             void setYpos(int iy) { y = iy; }
             void setXpos(int ix) { x = ix; }
             void setName(string sName2) { sName = sName2; }
             void setImage(string dir) throw(exception);
             string getName() { return sName; }
      
      private:
              int x, y;
              string sName;
};
#endif
And here is the main.cpp
#include <string>
#include <SDL/SDL.h>
#include <exception>
#include <iostream>
#include "CPlayer.h"

SDL_Surface *screen,
            *playerRight,
            *playerRightShoot,
            *playerLeftShoot,
            *playerLeft,
            *deadRight,
            *deadLeft,
            *fire,
            *intro;

int main(int argc, char *argv[])
{
    using namespace std;
    if(screen == NULL)
    {
         cerr << "Error setting videomode";
         return 0;
    }
    
    playerRight      = SDL_LoadBMP("data/playerRight.bmp");
    playerRightShoot = SDL_LoadBMP("data/playerRightShoot.bmp");
    playerLeftShoot  = SDL_LoadBMP("data/playerLeftShoot.bmp");
    playerLeft       = SDL_LoadBMP("data/playerLeft.bmp");
    fire             = SDL_LoadBMP("data/fire.bmp");
    deadRight        = SDL_LoadBMP("data/deadRight.bmp");
    deadLeft         = SDL_LoadBMP("data/deadLeft.bmp");
    intro            = SDL_LoadBMP("data/intro.bmp");
    
    if(playerRight == NULL || playerLeft == NULL || fire == NULL || 
       deadRight == NULL || deadLeft == NULL || intro == NULL || playerRightShoot == NULL ||
       playerLeftShoot == NULL)
    {
         cerr << "Error loading data/*";
         return 0;
    } 
    
    return 0;
}
And the errors In file included from main.cpp:5: CPlayer.h:11: error: expected unqualified-id before ')' token CPlayer.h:16: error: variable or field `setName' declared void CPlayer.h:16: error: expected `;' before '(' token CPlayer.h:17: error: expected `;' before "void" CPlayer.h:17: error: variable or field `setImage' declared void CPlayer.h:17: error: expected `;' before '(' token CPlayer.h:18: error: `string' does not name a type CPlayer.h:22: error: `string' does not name a type CPlayer.h:9: error: an anonymous union cannot have function members CPlayer.h:23: error: abstract declarator `<anonymous class>' used as declaration CPlayer.h:23: error: namespace-scope anonymous aggregates must be static EDIT: shouldnt [ code ] [/ code ] work ? :) With out the spaced then :P

Share this post


Link to post
Share on other sites
Advertisement
You can't compile a header file. You get no errors because it is not compiled (at least most IDEs won't do anything with it - of course you can force the compiler to do it, but that's beside the point for this topic).

Change your inclusion guard to something else. Currently, every mention of CPlayer will be reduced to nothing by the pre processor.

Share this post


Link to post
Share on other sites
Quote:
Original post by BitMaster
You can't compile a header file. You get no errors because it is not compiled (at least most IDEs won't do anything with it - of course you can force the compiler to do it, but that's beside the point for this topic).

Change your inclusion guard to something else. Currently, every mention of CPlayer will be reduced to nothing by the pre processor.


Are you refering to #ifndef CPlayer #define CPlayer? Anyways i changed it to _CPlayer_ :) And it seems to work, would you care to explain why it will be reduced to nothing, why i cant have the same name as the class?

Share this post


Link to post
Share on other sites
Whenever the preprocessor encounters the string "CPlayer" it replaces it with whatever if you use
#define CPlayer whatever
. As whatever is an empty string in your case, every occurence of CPlayer is removed from your code. Usually, inclusion guards are derived from the file name, like Player_h. Avoid prefixing any names with _ or __. It's bad style, they are reserved for compiler (vendor) specific stuff.

Share this post


Link to post
Share on other sites
Look at this code:

#define CPlayer

class CPlayer {...}

after the preprocessor is run on it, it will become

class {...}

Because you have #defined CPlayer to be nothing.



Share this post


Link to post
Share on other sites
Don't name stuff starting with underscores, either, to be safe.

My convention (and it seems to be pretty commonly used) would dictate a name of CPLAYER_H for the include guard.

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!