-2
using namespace std;

class Student{
    public:
        Student(int test)
        {
            if(test == key)
            {cout << "A student is being verified with a correct key: "<< test << endl;}
        }
        private:
        int key= 705;
};



int main()
{
    int testkey;
    cout << "Enter key for Bob: ";
    cin >> testkey;

    Student bob(testkey);
}

所以我尝试运行它,但它说 C++ 无法为键分配值“错误使键成为静态”。我不知道这是什么意思:(

4

1 回答 1

1

类成员初始化器是 C++11 的一个特性,否则你必须在构造函数中初始化它。

class Student {
public:
    Student(int test)
    : key(705) {
   // ^^^^^^^^
        if(test == key)
            cout << "A student is being verified with a correct key: "<< test << endl;
    }

private:
    int key;
};
于 2013-06-18T12:33:21.240 回答