2015-02-08 186 views
1

我做了一個類。頭文件是:C++ Visual Studio類錯誤標識符字符串未定義

#pragma once 
#include <string> 
class Player 
{ 
public: 
    Player(); 
private: 
}; 

和cpp文件是:

#include "Player.h" 
#include <iostream> 
Player::Player() 
{ 
} 

當我定義在頭文件中的字符串並添加參數在頭文件中的一切玩家功能工作正常

#pragma once 
#include <string> 
class Player 
{ 
public: 
    Player(string name); 
private: 
    string _name; 
}; 

但是當我相同的參數添加到播放器的功能在cpp文件

#include "Player.h" 
#include <iostream> 
Player::Player(string name) 
{ 
} 

我收到一個錯誤:標識符「字符串」是未定義的,我也在頭文件中得到了同樣的錯誤,所以它也影響到了這一點。我試圖在cpp文件中包含字符串以期解決問題,但它不起作用。我非常渴望解決方案,夥計們。

+2

它遺漏之前'string'的命名空間。你嘗試過'std :: string'嗎? http://www.cplusplus.com/reference/string/string/ – Caramiriel 2015-02-08 14:06:50

+0

我懷疑標題更改是否按您所說的那樣工作! – 2015-02-10 15:07:45

回答

6

所有的STL類型,算法等在std命名空間內聲明。

爲了使你的代碼編譯,string類型也應指定命名空間:

Player(std::string name); /* Most recommended */ 

using namespace std; 
Player(string name); /* Least recommended, as it will pollute the available symbols */ 

using std::string; 
Player(string name); 
+1

_'使用命名空間標準;'_顫抖! – 2015-02-08 14:29:51

+1

@momomo http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list NOT教程! – 2015-02-10 15:08:06

相關問題