我收到错误:标识符"字符串"未定义.
但是,我包括string.h,在我的主文件中,一切正常.
码:
#pragma once
#include <iostream>
#include <time.h>
#include <string.h>
class difficulty
{
private:
int lives;
string level;
public:
difficulty(void);
~difficulty(void);
void setLives(int newLives);
int getLives();
void setLevel(string newLevel);
string getLevel();
};
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么会这样吗?
Pup*_*ppy 81
<string.h>
是旧的C头.C++提供<string>
,然后它应该被称为std::string
.
你忘记了你所指的命名空间。添加
using namespace std;
一直避免 std::string 。
因为string
在命名空间中定义std
.更换string
用std::string
,或加
using std::string;
Run Code Online (Sandbox Code Playgroud)
低于你的include
线.
它可能适用,main.cpp
因为其他一些标题中有这一using
行(或类似的东西).
小智 5
您必须使用 std 命名空间。如果这段代码在 main.cpp 中你应该写
using namespace std;
Run Code Online (Sandbox Code Playgroud)
如果此声明位于标头中,则不应包含名称空间而只需编写
std::string level;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
107014 次 |
最近记录: |