C++ Visual Studio 类错误标识符字符串未定义

标签 c++

我上了一个类。头文件是:

   #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/

相关文章:

c++ - Constexpr 可构造函数对象

c++ - std::unique_ptr 析构构造函数顺序

c++ - MinGW 中 GetBuffer 和 ReleaseBuffer 的 CString 方法有哪些替代方法?

c++ - 带有 move 语义的源函数和汇函数的签名

c++ - 最小正数的最简洁/最快算法

c# - 如何在 C# 中打印 n 次 n 而无需循环、递归或 goto

c++ : error: member access into incomplete type , 未使用的参数 [-Werror,-Wunused-parameter]

c++ - c++/c++11 中的 bool()、int() 和 double() 是什么?

c++ - 如何遍历 C++ 类的所有子类(在编译时)?

c++ - 如果 CPU 使用率不是 100%,鼠标坐标就会滞后——真的很奇怪!