Redefinition error with multiple derived classes in the main - c++

I have an Object base class, and I have several derived classes called Item, Person, and Location.
Because each of these are derived from Object I need to include Object.h in each of their header files, and I include all of the derived classes in my main.
Because I am doing that I am getting a redefinition error.
What I want to know is what is the correct way to include these files to avoid this error?
Thanks!
EDIT:
object.h
using namespace std;
class Object{
string name;
string description;
public:
Object();
Object(string name, string description);
void set_name(string name);
void set_description(string description);
string get_name();
string get_description();
~Object();
};
item.h
using namespace std;
#include "object.h"
class Item : public Object{
public:
Item();
Item(string name, string description);
};
locale.h
using namespace std;
#include "object.h"
class Locale : public Object{
public:
Locale();
Locale(string name, string description);
};
main.cpp
#include <iostream>
#include <string>
#include "locale.h"
#include "item.h"
using namespace std;
int main(){
return 0;
}

Strange, everybody I've met that hits this problem does not have a slightest idea what is going on and you have properly analysed the problem.
Read this:
http://en.wikipedia.org/wiki/Include_guard

You should add include guards to your headers. This prevents headers from being included twice. For example, at the the top of the Object.h header, you would put,
#ifndef _OBJECT_H
#define _OBJECT_H
and then you end the header with,
#endif
If the header has already been included, the text between #ifndef and #endif is dropped.

If you haven't got them in place already, you need to put include guards into you header files to prevent including the same files multiple times (which would redefine the classes).

Related

C++ Why Is There "Unknown Type" When Class Header is Included? [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 6 years ago.
I have this header file and I'm trying to make variables of type Item. I've included #include "Item.h" and yet I still get a unknown type name Item error on both private variables when I compile.
#ifndef PLAYER_H
#define PLAYER_H
#include <vector>
#include "Item.h"
using std::vector;
class Player
{
public:
// constructor
Player( void );
// destructor
virtual ~Player( void );
private:
Item item;
std::vector <Item> inventory;
};
#endif /* PLAYER_H */
Whats up with this?
Heres the Item.h that I'm including
#ifndef ITEM_H
#define ITEM_H
#include <string>
#include "Player.h"
#include "GlobalDefs.h"
class Item {
public:
Item();
Item(gold_t v, std::string n);
virtual ~Item();
// Getter
inline virtual gold_t GetValue (void)
{
return value;
}
// Getter
inline virtual std::string GetName (void);
// Getter
virtual std::string GetItemText(void);
protected:
gold_t value;
std::string name;
};
#endif /* ITEM_H */
If you include Item.h from your cpp file, Player.h is included from it. Then, Player.h includes Item.h again, but thanks to the include guard, this does virtually nothing.
Then, in the included Player.h, no Item is declared yet. Therefore, the compiler will emit the error.
Since nothing from Player.h is used in Item.h, remove #include "Player.h" from Item.h.
You're including "Player.h" in "Item.h" and make it circular dependency. Since it's not necessary at all so just remove it.

C++ Visual Studio class error identifier string is undefined

I made a class. the header file is :
#pragma once
#include <string>
class Player
{
public:
Player();
private:
};
and the cpp file is :
#include "Player.h"
#include <iostream>
Player::Player()
{
}
When I define a string in the header file and add an argument to the Player function in the header file everything works fine
#pragma once
#include <string>
class Player
{
public:
Player(string name);
private:
string _name;
};
but when I add the same argument to the Player function in the cpp file
#include "Player.h"
#include <iostream>
Player::Player(string name)
{
}
I get an error: identifier "string" is undefined and I get the same error in the header file as well so it effects that too. I tried including string in the cpp file in hopes of solving the problem but it did not work. I'm really desperate for a solution, guys.
All STL types, algorithms etc are declared inside std namespace.
To make your code compile, string type should also specify the namespace as:
Player(std::string name); /* Most recommended */
or
using namespace std;
Player(string name); /* Least recommended, as it will pollute the available symbols */
or
using std::string;
Player(string name);

C++: Inclusion inside inclusion

The title might not be very clear, it's a bit more complex than that. I searched the web for something like my problem but I did not find anything that could help me.
This is not about infinite looping inclusions, I already put preprocessor directives to avoid that.
I have two classes Monster and Character, respectively declared in their own header files, monster.hpp and character.hpp, and respectively implemented in their own source files, monster.cpp and character.cpp.
The problem now is that both classes need each other to work.
monster.hpp :
#ifndef INCLUDE_MONSTER_HPP
#define INCLUDE_MONSTER_HPP
#include "character.hpp"
class Monster
{
private: //Atributes
public: //Methods
void attackM(Monster& id);
void attackC(Character& id);
};
#endif //MONSTER_HPP_INCLUDED
character.hpp :
#ifndef INCLUDE_CHARACTER_HPP
#define INCLUDE_CHARACTER_HPP
#include "monster.hpp"
class Character
{
private: //Attributes
public: //Methods
void attackM(Monster& id);
void attackC(Character& id);
};
#endif //CHARACTER_HPP_INCLUDED
and the main.cpp :
#include <iostream>
#include "character.hpp"
#include "monster.hpp"
using namespace std;
int main(int argc, char **argv)
{
//Whatever
return 0;
}
And I get this error from the compiler :
In file included from character.hpp:7:0,
from main.cpp:3:
monster.hpp:24:16: error: 'Character' has not been declared
void attackC(Character& id);
(the line and column numbers may be wrong)
From what I understand, when monster.hpp is included into character.hpp, the compiler sees that the class Monster uses the class Character, which is not declared yet right at the moment when monster.hpp is included into character.hpp.
And I don't know how to fix that.
Any ideas ?
The way this works is that the header files of char and monster do not include each other. Instead you forward declare the classes and include the headers within the CPP files.
So basically replace #include "monster.hpp" in the.h with class Monster; and #include "monster.hpp" in the .cpp - and same for the other class.
See this question for more details:
What are forward declarations in C++?
#ifndef INCLUDE_MONSTER_HPP
#define INCLUDE_MONSTER_HPP
#include "character.hpp"
class Character;
class Monster
{
private: //Atributes
public: //Methods
void attackM(Monster& id);
void attackC(Character& id);
};
#endif //MONSTER_HPP_INCLUDED
You need to use a forward declaration. See: http://en.wikipedia.org/wiki/Forward_declaration
Basically, the compiler doesn't know what a "Character" is. You temporarily indicate that it's something you can point to by adding the following stub to Character.hpp:
class Character;
Use predeclaration in *.h:
class Character;
class Monster;
use in *cpp the includes:
#include "character.h"
#include "monster.h"
Or put everything in one *.hpp with predeclaration.

Circular Dependency c++

I have the following header files within my code. I know that the problem is that a circular dependency is occurring but I can't seem to solve it.
Any help to fix it?
project.h gets me this ERROR: field ‘location’ has incomplete type
#ifndef PROJECT_H_
#define PROJECT_H_
#include <string.h>
#include "department.h"
class department;
class project{
string name;
department location;
public:
//constructors
//Setters
//Getters
};
#endif
employee.h gets me this ERROR field "‘myDepartment’ has incomplete type"
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include "department.h"
#include <vector>
class department;
class project;
class employee
{
//attributes
department myDepartment;
vector < project > myProjects;
public:
//constructor
// Distructor
//Setters
//Getters
#endif
department.h
#ifndef DEPARTMENT_H_
#define DEPARTMENT_H_
#include <string.h>
#include "employee.h"
#include "project.h"
#include <vector>
class project;
class employee;
class department{
private:
string name;
string ID;
employee headOfDepatment;
vector <project> myprojects;
public:
//constructors
//Setters
//Getters
};
#endif
You have cyclical #includes.
Try removing #include "employee.h" and #include "project.h" from department.h.
Or vice versa.
You have an include tree like this which will cause you
problems:
project.h
department.h
employee.h
department.h
department.h
employee.h
project.h
normally it is better to make your headers as independent of
other class headers as possible, to do this keep your forward
declarations but remove the includes, then in the .cpp file
include the headers.
e.g.
class project;
class employee;
class department {
...
employee* headOfDepartment;
vector<project*> myprojects;
then in department.cpp
include employee.h and project.h and instantiate the members in your constructor, to make it even better use unique_ptr so you don't need to bother about deleting them:
class department {
...
std::unique_ptr<employee> headOfDepartment;
std::vector<std::unique_ptr<project>> myprojects;
another tip is to not have using namespace std in the header, instead include the namespace e.g. std::vector<...>

linux ubuntu c++ .h and .cpp file

I have a my.h file:
#ifndef __MY__
#define __MY__
#include <string>
#include <time.h>
class S
{
public: S();
std::string myname;
};
#endif
my.cpp
#include "my.h";
#include<string>
#include<iostream>
using namespace std;
S::S()
{
// .. code
}
I want to create an so file. There is no error when creating it. But when I compile the .h file it says: string:No such file or directory. If I pus string.h instead of string I have the error: expected '=',',',';','asm', before S (at class S) in my.h.
In the .cpp file (if i change the string with string.h) i have after i compile error: string in namespace std does not name a type. WHERE AM I WRONG?
Well, first, it seems that you come from java because when you typed:
class S
{
public: S();
std::string myname;
};
I guess you actually meant:
class S
{
public:
S();
private:
std::string myname;
};
In the .cpp file, you typed s instead of S: note that C++ is case-sensitive regarding classes names.
Also, regarding your problem, I suspect you are currently using a C compiler and not a C++ compiler. Without knowing the used command-line, I can't say much more on that.
Try this
#ifndef MY_H
#define MY_H
#include <string>
#include <time.h>
class S
{
public: S();
std::string myname;
};
#endif
#include "my.h"
#include<string>
#include<iostream>
using namespace std;
S::S()
{
//code
}