C++错误(操作数类型为"std::string 和 'void")

C++ Error (operand types are 'std::string and 'void')

本文关键字:void string std 错误 操作数 类型 C++      更新时间:2023-10-16

我是c++编程新手。所以,如果我在提问时犯了很多错误,请向我道歉。

我的问题是

创建一个包含私有变量和方法的类,如下所示:

class Records{
private:
    string name;
public:
    string n;
    void setValue(){
          cout << "Enter name" << endl;
          cin >> name;
     }
    void getValue(){
         n = name;
        cout << "Name is: " <<  n << endl;
    }
};

我有运行你的代码在GCC编译器上,它成功地工作了。请参阅完整示例:

#include <iostream>
using namespace std;
class Records{
        private:
                string name;
        public:
                string n;
                void setValue(){
                        cout << "Enter name" << endl;
                        cin >> name;
                }
                void getValue(){
                        n = name;
                        cout << "Name is: " <<  n << endl;
                }
};
int main()
{
        Records r;
        r.setValue();
        r.getValue();
}
相关文章: