#include <string> not working?

This is a discussion on #include <string> not working? within the C++ Programming forums, part of the General Programming Boards category; Code: #include "stdafx.h" #include "Player.h" #include <iostream> #include <string> using namespace std; int main(){ string name; Player PlayerInfo; cout <<"Enter ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    17

    #include <string> not working?

    Code:
    #include "stdafx.h"
    #include "Player.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main(){
    	string name;
    	Player PlayerInfo;
    	
    	cout <<"Enter a name"<<endl;
    	cin >> name;
    	//PlayerInfo.setPlayerName(name);
    	system("pause");
    
    	return 0;
    }
    I get an error saying 'name' : undeclared identifier. Also, I get an error for any class member function that uses a string as either a return type or a parameter.

    Here is the code for my class.

    Code:
    class Player{
    public:
    	void setPlayerName(string);
    	string getPlayerName();
    	void setMaxHP(int newHP);
    	int getMaxHP();
    	void setCurrentHP(int value);
    	int getCurrentHP();
    	void setCurrentMP(int value);
    	int getCurrentMP();
    	void setIntel(int newIntel);
    	int getIntel();
    	void SetGold(int newGold);
    	int getGold();
    	void setStr(int newstr);
    	int getStr();
    	void setAgil(int newAgil);
    	int getAgil();
    	void setAP(int newAP);// AP = Attack power
    	int getAP();
    	void setXP(int newXP);
    	int getXP();
    	void setSP(int newSP); //SP = spell power
    	int getSP();
    	void setLevel(int newLevel);
    	int getLevel();
    private:
    	int MaxHP;
    	int MaxMP;
    	int currentHP;
    	int currentMP;
    	int gold;
    	int str;
    	int intel;
    	int agil;// agility
    	int AP; // attack power
    	int SP; // spell power
    	int XP;
    	int level;
    	int PlayerState;
    	string PlayerName;
    	string Spells[14];
    	string Abilities[11];
    	string castedSpell;
    	string usedAbility;
    };
    void Player::setPlayerName(string name){
    	name = PlayerName;
    }
    string Player::getPlayerName(){
    	return PlayerName;
    }
    int Player::getAgil(){
    	return agil;
    }
    int Player::getLevel(){
    	return level;
    }
    int Player::getMaxHP(){
    	return MaxHP;
    }
    int Player::getStr(){
    	return str;
    }
    int Player::getXP(){
    	return XP;
    }
    int Player::getGold(){
    	return gold;
    }
    int Player::getCurrentHP(){
    	return currentHP;
    }
    int Player::getIntel(){
    	return intel;
    }
    
    int Player::getSP(){
    	return SP;
    }
    
    int Player::getAP(){
    	return AP;
    }
    int Player::getCurrentMP(){
    	return currentMP;
    }
    int Player::getIntel(){
    	return intel;
    }
    int Player::getAgil(){
    	return agil;
    }
    
    void Player::setMaxHP(int newMaxMP){
    	 newMaxMP = MaxMP;
    }
    
    
    void Player::setAgil(int newAgil){
    	newAgil = agil;
    }
    void Player::setLevel(int newLevel){
    	newLevel = level;
    }
    void Player::setMaxHP(int newMaxHP){
    	newMaxHP = MaxHP;
    }
    void Player::setStr(int newstr){
    	newstr = str;
    }
    void Player::setXP(int newXP){
    	newXP = XP;
    }
    void Player::SetGold(int newGold){
    	newGold = gold;
    }
    void Player::setCurrentHP(int newHealth){
    	newHealth = CurrentHP;
    }
    void Player::setIntel(int newIntel){
    	newIntel = intel;
    }
    void Player::setAP(int newAP){
    	newAP = AP;
    }
    void Player::setSP(int newSP){
    	newSP = SP;
    }
    void Player::setCurrentMP(int newMana){
    	newMana = CurrentMP;
    }
    I #include <string> so I don't know why I am getting these errors.

    Code:
    1> error C2061: syntax error : identifier 'string'
    1> error C2146: syntax error : missing ';' before identifier 'getPlayerName'
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>warning C4183: 'getPlayerName': missing return type; assumed to be a member function returning 'int'
    1>error C2146: syntax error : missing ';' before identifier 'PlayerName'
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1> error C2146: syntax error : missing ';' before identifier 'Spells'
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>error C2146: syntax error : missing ';' before identifier 'Abilities'
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>error C2146: syntax error : missing ';' before identifier 'castedSpell'
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1> error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    ...more of the same, every time I use string as an identifier it gives me an error

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Did you #include <string> and specify the std namespace in Player.h? If not, you should have because you use string inside that file.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    You should #include <string> in Player.h and fully qualify it as std::string, at least in Player.h and all other header files. Player.h should also have an inclusion guard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    17
    Thanks, that did it.
    Player.h should also have an inclusion guard.
    What exactly is that?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Something like this:
    Code:
    #ifndef PLAYER_H
    #define PLAYER_H
    
    // Player class definition and related declarations.
    // ...
    
    #endif
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Besides, why don't you just make those members public instead of creating get/set methods for all of them ? It looks nasty (to me). I know some will say that it will make things easier if you need to modify the behaviour of a variable assignment (for example resizing a window after setting its new size) but I prefer this to having to write twice the code with 98&#37; of it being useless and redundant.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Desolation View Post
    Besides, why don't you just make those members public instead of creating get/set methods for all of them ? It looks nasty (to me). I know some will say that it will make things easier if you need to modify the behaviour of a variable assignment (for example resizing a window after setting its new size) but I prefer this to having to write twice the code with 98% of it being useless and redundant.
    I'd replace the entire thing with a std::map<std::string, ability> with ability being a class representing the abstract value of an ability. Then the abilities would be accessed directly by name instead of through a bunch of different functions which are all identical. Also, adding a new ability would not involve a change to Player.h, which would force a recompile of most of the codebase (I assume Player.h would probably be included almost everywhere).

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    If all of your get() functions just return the private member, set them to const:

    Code:
    class Foo
    {
    public:
       unsigned short getHp() const;
    };
    
    unsigned short Foo::getHp() const
    {
       return m_Hp;
    }
    The small change ensures the function cannot modify the value being returned even
    if it wanted to. Its just a bit of extra debugging gaurd
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  2. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  3. Boost thread not working
    By Monkeymagic in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2006, 01:50 AM
  4. Header file include order
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2006, 03:22 PM
  5. #include <string>
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-04-2002, 09:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21