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);
Related
Pretty simple question but I couldn't find an answer regarding this specific question which surprised me.
I get a string of errors when attempting to call a class function that changes a private class string.
Edit: I have solved the problem - I forgot to include the required namespace and assembly references in the header file.
Here is the .h file code:
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
public:
Animal();
~Animal();
string getName();
void setName(string animalName);
private:
string name;
};
#endif
here is the class .cpp:
#include "Animal.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
Animal::Animal()
{
}
Animal::~Animal()
{
}
void Animal::setName(string animalName)
{
name = animalName;
}
string Animal::getName()
{
return name;
}
finally,here is the int main(),where I have attempted to call the functions (I just got a bunch of errors upon compiling)
int main()
{
Animal chicken;
chicken.setName("gary");
cout << chicken.getName() << endl;
_getch();
}
Error messages include:
error C2061: syntax error : identifier 'string'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
`error C2146: syntax error : missing ';' before identifier 'getName'`
Looks like you forgot to include <string> in your header. The string object also lives in the std namespace so you need provide a fully qualified name to use it (don't add using namespace to headers).
#ifndef ANIMAL_H
#define ANIMAL_H
#include <string> // You need to include this
class Animal
{
public:
Animal();
~Animal();
std::string getName();
void setName(std::string animalName);
private:
std::string name;
};
#endif
Your class header is missing string library declaration and std:: before each string declaration.
#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
class Animal
{
public:
Animal();
~Animal();
std::string getName();
void setName(std::string animalName);
private:
std::string name;
};
#endif
#edit
Up
You beat me to it! Your answer showed up just when I posted mine
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.
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<...>
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).
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
}