Hey guys i am having trouble with creating a Parent class(gameobject) for two other child classes(player,location) both of the child classes can access data from the parent class, and work well independently but once i include the h file for the child classes together in main.cpp i get the compiler error stating base class unidentified and class type redefinition is visual c++ what am i doing wrong ? sorry for the formating

//BASE CLASS OR PARENT CLASS

#include <iostream>
#include <string>

using namespace std;

class GameObject
{
 public :   
    string gamename;
    string gametype;
    string gamesize;

   //default constructor

    GameObject()
   {
       gamename = " Beta ";
       gametype = " Not defined ";
       gamesize = " Not defined ";
   }

   //primary constructor
   GameObject(string name,string type,string size)
   {
       gamename = name;
       gametype = type;
       gamesize = size;
   }

   //copy constructor
   GameObject(GameObject &obj)
   {

       gamename = obj.gamename; 
       gametype = obj.gametype;
       gamesize = obj.gamesize;
   }


   // Input Data
   void GetGame()
   {
       cout << " Enter Game Name : ";
       getline(cin,gamename);

       cout << " Enter Game Type : ";
       getline(cin,gametype);

       cout << " Enter Game Size : ";
       getline(cin,gamesize);

       cout << "\n\n";
   }

   // Print Data

   void PrintGame()
   {

       cout << " Game Name : " << gamename << endl;
       cout << " Game Type : " << gametype << endl;
       cout << " Game Size : " << gamesize << endl;

       cout << "\n\n";
   }

}; // END OF CLASS




// CHILD CLASS TO PARENT CLASS GAMEOBJECT

#include <iostream>
#include <string>

#include "GameObject.h"

using namespace std;

class Player : public GameObject
{
public:

    string playername;
    string playerrole;
    string playerstrength;


    //Default Constructor
    Player()
    {
        playername = " ";
        playerrole = " ";
        playerstrength = " ";
    }

    //Primary Constructor
    Player(string pname,string prole,string pstren)
    {
        playername = pname;
        playerrole = prole;
        playerstrength = pstren;
    }

    //Copy Constructor
    Player(Player &obj)
    {
        playername = obj.playername;
        playerrole = obj.playerrole;
        playerstrength = obj.playerstrength;
    }



    //Player Input
    void GetPlayer()
    {
        cout << " Enter Player Name : ";
        getline(cin,playername);

        cout << " Enter Player Role : ";
        getline(cin,playerrole);

        cout << " Enter Player Strength : ";
        getline(cin,playerstrength);


        cout << "\n\n";
    }

void PrintPlayer()
    {

        cout << " Player Name : " << playername << endl;
        cout << " Player Role : " << playerrole << endl;
        cout << " Player Strength : " << playerstrength << endl;

        cout << "\n\n";
    }


};// END OF CLASS



// Child Class 2 to Parent Class GameObject

#include <iostream>
#include <string>

#include "GameObject.h"

using namespace std;

class Location : public GameObject
{
public:

    string locationname;
    string climate;
    string terrain;

    Location()
    {
        locationname = " ";
        climate = " ";
        terrain = " ";
    }

    //Primary Constructor
    Location(string locname,string climate_c,string terrain_c)
    {
        locationname = locname;
        climate = climate_c;
        terrain = terrain_c;
    }

    //Copy Constructor
    Location(Location &obj)
    {
        locationname = obj.locationname;
        climate = obj.climate;
        terrain = obj.terrain;
    }


    //Get Location Data
    void GetLocation()
     {
         cout << " Location : ";
         getline(cin,locationname);

         cout << " Climate : ";
         getline(cin,climate);

         cout << " Terrain : ";
         getline(cin,terrain);

     }

    //Print Location Data
    void PrintLocation()
    {
        cout << " Location : " << locationname << endl;
        cout << " Climate  : " << climate << endl;
        cout << " Terrain  : " << terrain << endl;

        cout << "\n\n";
    }



};//END OF CLASS


//Main.cpp
#include <iostream>
#include <string>

#include "Player.h"
//#include "Location.h"

using namespace std;

void main()
{

    Player playa1;
    // Location kgn;

    playa1.GetGame();
    playa1.GetPlayer();
    // kgn.GetLocation();


    playa1.PrintGame();
    playa1.PrintPlayer();
    // kgn.PrintLocation();

    system("pause");
}

try removing iostream from header files

In your header files put;

At the top of the file,

 #ifndef CLASSNAME_H
 #define CLASSNAME_H

At the bottom of the file after all the code.

#endif

This ensures that the class is only defined once. Having multiple includes for the same header file often causes linking issues.

ok thanks for the input that worked thanks

@mike2k
Yeah, I break that rule all the time! :-) Hasn't bit me in the rear-end, yet... But I do understand the rationale why it is "not a good idea". Just lazy I guess! My upvote for your last post speaks for itself.

Thanks Mike. Good/great advice!

noted

Good programming practice is that in inheritance you should keep the data members of parent class 'protected' means you should use proctected access specifier.
This allows only the child classes to access the data members of parent class while keeping data memebers public kills the main working of OOP.
Always use getters and setters funtions for data memebers else your data abstraction is no more data abstraction :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.