c++ - 在 C++ 中对类属性使用字符串数据类型的问题

标签 c++ string properties

我目前正在用 C++ 为一个类定义一些属性,但是在使用类型 string 而不是 int。例如:

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

文件(标题)看起来像这样:

#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

如果有人可以就我做错的事情提供帮助或提供处理字符串属性的更好方法(就像我之前提到的那样,其他类型可以正常工作),我们将不胜感激。谢谢。

最佳答案

stringstd 命名空间的一部分。

您必须使用 std::string 而不是 string 或添加 using namespace std; (我不建议您在你的头文件,阅读 "using namespace" in c++ headers )。

关于c++ - 在 C++ 中对类属性使用字符串数据类型的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127002/

相关文章:

c# - 将\u2265 插入 C# 字符串

java - 如何在java中使用indexOf(int ch)

c++ - 使用 CUDA 线程索引作为数字

c++ - 处理 "Thrown exception type is not nothrow copy constructible"警告

c++ - 编译后未链接共享对象库

c++ - 为什么 std::vector 的构造函数接口(interface)用 C++11 改变了?

c# - 如何拆分包含 "[*word*]"的字符串

xcode - 如何将 GIDSignInButton 类与 Google 身份验证结合使用?

javascript - Javascript 中的函数作为对象

java - 如何使用注释在 Spring 4 中重新加载属性文件?