我目前正在为C++中的类定义一些属性,但是在使用string
类型而不是int
或double
时遇到了麻烦。例如:
private:
int LOT;
public:
int getLOT() {
return LOT;
}
void setLOT(int value) {
LOT = value;
}
工作正常,但是:
private:
string name;
public:
string getName() {
return name;
}
void setName(string value) {
name = value;
}
引发以下错误:
https://s26.postimg.org/wm5y7922h/error.png
该文件( header )如下所示:
#include "general.h" // a header which includes all my other #includes
// which, yes, does include <string>
class MyClass
{
private:
string name;
public:
string getName() {
return name;
}
void setName(string value) {
name = value;
}
// other properties similar to the above
}
目的是像这样访问变量:
cout << "Enter your name: ";
cin >> MyClass.setName();
cout << "\nHello, " << MyClass.getName();
// although this isn't exactly how it'll be used in-program
如果有人可以为我做错的事情提供帮助,或者提供更好的方式处理字符串属性(如我之前提到的,其他类型也可以),将不胜感激。谢谢。
最佳答案
string
是std
命名空间的一部分。
您必须使用std::string
而不是string
或添加using namespace std;
(我不建议您在头文件中进行此操作,请阅读"using namespace" in c++ headers)。