Sign in to follow this  
DizzyKuma

Inheritance Error: expected class-name before '' token

Recommended Posts

Hi, I've been travelling around Google all day and so far nothing like this error has been solved. Not sure what's wrong with this but it only comes up when a subclass is created. Please help! :o Base class (game.h):
//#include "gameplatform.h"
#ifndef GAME_H_
#define GAME_H_


class Game
{
    
      public: 
              void setGame(char gamename[], char gameplatform[], char gamerating[], float gameprice);
              char* getName();
              char* getPlatform();
              char* getRating();
              float getPrice();
      
      
      private:
              
              char gname[25];
              char gplatform[25];
              char grating[10];
              float gprice;
      
};

#endif


game.cpp:
#include <cstdlib>
#include <iostream>
#include "game.h"
#include <stdio.h>
#include <string.h>

void Game::setGame(char gamename[], char gameplatform[], char gamerating[], float gameprice)
{
     //Copy the array to another variable
     strcpy(gname,gamename);
     strcpy(gplatform,gameplatform);
     strcpy(grating,gamerating);
     gprice = gameprice;
     
}

char* Game::getName()
{
      //Return this variable
      return gname;
}

char* Game::getPlatform()
{
      //Return this variable
      return gplatform;
}
char* Game::getRating()
{
      //Return this variable
      return grating;
}

float Game::getPrice()
{
      //Return this variable
      return gprice;
}
Subclass(gameplatform.h):
#ifndef GAMEPLATFORM_H_
#define GAMEPLATFORM_H_
//#include "game.h"

class gameplat: public game
{
      
      public:
             void thisfunction();
             
      private:
      
};





#endif /*For Game Platform*/

gameplatform.cpp:
#include "gameplatform.h"
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>

void thisfunction()
{
}

The int main() is currently empty. I tried to make it work without any code just:
system("PAUSE");
    return EXIT_SUCCESS;
Every times it compiles it comes up with the same error. :( [Edited by - DizzyKuma on June 4, 2009 11:38:19 AM]

Share this post


Link to post
Share on other sites
game.h

#ifndef GAME_H_
#define GAME_H_

// In C++, we use std::string instead of char arrays
#include <string>

class Game
{
public:
void setGame( const std::string& gamename, const std::string& gameplatform, const std::string& gamerating, float gameprice );

// Return const references to our strings instead of the entire string
const std::string& getName();
const std::string& getPlatform();
const std::string& getRating();
float getPrice();


private:
std::string gname;
std::string gplatform;
std::string grating;
float gprice;
};

#endif



game.cpp

#include "game.h"
// #include <iostream> // Only include headers you're using

void Game::setGame( const std::string& gamename, const std::string& gameplatform, const std::string& gamerating, float gameprice ) :
// You can construct member variables here in what's called an initializer-list:
gname( gamename ), // this copies the string referenced from gamename to gname
gplatform( gameplatform ),
grating( gamerating ),
gprice( gameprice )
{

}

const std::string& Game::getName()
{
return gname;
}

const std::string& Game::getPlatform()
{
return gplatform;
}
const std::string& Game::getRating()
{
return grating;
}

float Game::getPrice()
{
return gprice;
}



gameplatform.h

#ifndef GAMEPLATFORM_H_
#define GAMEPLATFORM_H_

// In order for gameplat to inherit from Game, it needs to know
// all about Game before deriving from it :)
#include "game.h"

class gameplat : public Game
{
public:
void thisfunction();
};

#endif /*For Game Platform*/



gameplatform.cpp

#include "gameplatform.h"
// #include <cstdlib> // Only include header files you're using
// #include <iostream>
// #include <stdio.h>
// #include <string.h>

// I think you meant "gameplat::thisfunction()" instead of "thisfunction()"
void gameplat::thisfunction()
{
}



You can use [source][/source] tags to format your code. [smile]

EDIT:
I see that you have a system("PAUSE"); there, so I'll assume you're on a Windows platform. If you're not using it already, get Microsoft Visual C++ 2008 Express Edition.

[Edited by - _fastcall on June 4, 2009 11:09:20 AM]

Share this post


Link to post
Share on other sites
_fastcall beat me to and just fixed your code but that error means that the compiler couldn't figure out how to resolve the class name at compile time, hence is complaining about it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this