我上了一个类。头文件是:
#pragma once
#include <string>
class Player
{
public:
Player();
private:
};
cpp 文件是:
#include "Player.h"
#include <iostream>
Player::Player()
{
}
当我在头文件中定义一个字符串并向头文件中的 Player 函数添加一个参数时,一切正常
#pragma once
#include <string>
class Player
{
public:
Player(string name);
private:
string _name;
};
但是当我将相同的参数添加到 cpp 文件中的 Player 函数时
#include "Player.h"
#include <iostream>
Player::Player(string name)
{
}
我得到一个错误:标识符“字符串”未定义,我在头文件中也得到同样的错误,所以它也会影响。我尝试在 cpp 文件中包含字符串,希望能解决问题,但没有成功。伙计们,我真的很想找到解决方案。
最佳答案
所有 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);
关于C++ Visual Studio 类错误标识符字符串未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394815/