使用字符串时出现问题
Trouble using strings
我是个初学者。我不知道为什么我不能使用字符串。上面写着string does not have a type
。
main.cpp
#include <iostream>
#include <string>
#include "Pancake.h"
using namespace std;
int main() {
Pancake good;
good.setName("David");
cout << good.name << endl;
}
煎饼.h
#ifndef PANCAKE_H
#define PANCAKE_H
#include <string>
class Pancake {
public:
void setName( string x );
string name;
protected:
private:
};
#endif // PANCAKE_H
煎饼.cpp
#include <iostream>
#include "Pancake.h"
#include <string>
using namespace std;
void Pancake::setName( string x ) {
name = x;
}
只有当我使用字符串时才会发生这种情况。当我在string x
的所有实例中使用整数并用int x
替换string x
时,它就起作用了。但为什么呢?
您只需在头文件中省略名称空间:
#ifndef PANCAKE_H
#define PANCAKE_H
#include <string>
class Pancake {
public:
void setName( std::string x );
std::string name;
protected:
private:
};
#endif // PANCAKE_H
最好避免使用using namespace ...
,而是接受在名称空间前面加上前缀的额外类型。
相关文章:
- 编译包含字符串的代码时遇到问题
- setlocale的C++土耳其字符串问题
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 我的逻辑反转字符串中的元音有什么问题?
- 按字母顺序对C++问题中的子字符串索引进行分区
- 用户输入字符串的文件附加问题..C++
- 使用 minijson-reader 库读取 JSON 字符串时出现问题
- 在将字符串放入字符串向量时遇到问题?
- 将文件复制到自定义位置,存在字符串转换问题
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 不确定出了什么问题(字符串输入)
- 反向和计数字符串的 C++ 问题
- 反转具有返回值的字符串时出现问题
- C++字符串问题-如何访问字符串元素
- public:静态常量字符串声明/初始化问题
- 在MySql中使用字符串操作函数有什么问题
- Libwesockets.h:lws_write:C++字符串到C的转换和发送问题
- 问题:字符串在另一个字符串上写入
- 日志文件输出的问题-字符串和c_str之间的差异- c++