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

时间:2016-06-28 05:44:56

标签: c++ class object

首先是我在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;
    }

};

1 个答案:

答案 0 :(得分:0)

我在 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();
}