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
Related
So I have searched high and low and maybe I don't know what question I'm trying to ask, and if that's the case please help me figure out what that might be.
I am trying to print out the color of myCar object from the car class, using a function from the myMan object from the man class. I (think) I have the man class as a friend to the car class so I dont know why I cant access the cars private color variable. Can someone tell me why I can't access the cars private color variable? I have to use friend.
I have been trying to do this for more then 20 hours now and I'm at my wits end.I have included all the files I have because I have seen it done other ways (all in one file) and can't seem to reproduce it broken up like this.
Error 1 error C2433: 'man' : 'friend' not permitted on data declarations car.h line 14
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int car.h line 14
Error 3 error C2248: 'car::color' : cannot access private member declared in class 'car' man.cpp line 13
Error 4 error C2061: syntax error : identifier 'car' man.h line 16
Error 5 error C2660: 'man::printCarColor' : function does not take 1 arguments source.cpp line 16
Error 6 error C2061: syntax error : identifier 'car' man.h line 16
car.h
#pragma once
#include <iostream>
#include <string>
#include "man.h"
using namespace std;
class car
{
public:
car();
car(string);
~car();
friend man;
private:
string color;
};
car.cpp
#include "car.h"
car::car()
{
}
car::car(string c){
color = c;
}
car::~car()
{
}
man.h
#pragma once
#include <iostream>
#include <string>
#include "car.h"
using namespace std;
class man
{
public:
string name;
man();
man(string);
void printCarColor(car);
~man();
};
man.cpp
#include "man.h"
man::man()
{
}
man::man(string newname)
{
name = newname;
}
void man::printCarColor(car mycar){
cout << mycar.color;
}
man::~man()
{
}
source.cpp
#include <iostream>
#include <string>
#include "car.h"
#include "man.h"
using namespace std;
int main()
{
car myCar("green");
//cout << myCar.color;
man myMan("Jim");
myMan.name;
myMan.printCarColor(myCar);
system("pause");
return 0;
}
You have a circular include dependency. car.h includes man.h and man.h includes car.h. This cannot work. Remove #include "man.h" from car.h, use friend class man instead of friend man. – n.m.
** hope this isn't a bad way of answer this, credit goes to ^
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);
I'm not exactly sure why I'm getting these errors, I thought it was because I needed a forward declaration, but the problem still persists. I have no clue what is causing this. I put comments where the offending lines are. Looking for an explanation on why this is happening and a resolution. Thanks.
// Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "Weapon.h"
#include "Monster.h"
#include <string>
// Forward declaration
class Race;
class Player
{
public:
Player();
/* Return Type Method Name */
bool IsDead();
std::string GetName();
int GetArmor();
void TakeDamage(int damage);
void CreateClass();
bool Attack(Monster& monster);
void LevelUp();
void Rest();
void ViewStats();
void Victory(int xp, Monster* monster);
void GameOver();
void DisplayHitPoints();
void SetRace();
private:
/* Type Name */
std::string m_Name;
std::string m_ClassName;
int m_Accuracy;
int m_HitPoints;
int m_MaxHitPoints;
int m_ExpPoints;
int m_NextLevelExp;
int m_Level;
int m_Armor;
int m_Gold;
Weapon m_Weapon;
Race m_Race; // problem: error C2079 uses undefined class 'Race'
};
#endif // PLAYER_H
An offending method with a cannot convert from 'Race' to 'int' error
void Player::SetRace()
{
Race race;
m_Race = race.SelectRace(); // problem: error C2440 cannot convert from 'Race' to int
}
Race class definition:
// Race.h
#ifndef RACE_H
#define RACE_H
#include <string>
class Race
{
public:
Race();
/* Return Type Method Name*/
Race SelectRace();
protected:
std::string GetName();
/* Type Name*/
std::string m_Name;
int m_Accuracy;
int m_HitPoints;
};
// ...below here implements multiple derived classes of type race
#endif // RACE_H
Yes, as you've found, a forward declaration is insufficient here. That's because there is a member of type Race in Player.
Include the whole header at the top of player.h:
#include "Weapon.h"
#include "Monster.h"
#include "Race.h"
Then the definition of your Race class will be visible to the definition of Player.
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
}