我正在做一个大学项目,遇到了这个问题->当我声明bool a并在其中存储值true/false时,代码运行良好但是,虽然我只是声明,然后存储值,就像在代码中一样,它显示了一个错误,有人能解释为什么它是…吗。。。?
#include <iostream>
class Design{
public:
void welcome(){
std :: cout << "------------------------------------------------------------n";
std :: cout << " Welcome to Faculty Feedback form. Enter Your credentialsn";
std :: cout << "------------------------------------------------------------n";
}
};
class Admin: public Design{
};
class Student: public Admin{
};
class Login: public Student{
//bool a = true; Works fine
bool a;
a = true; // Shows error
public:
void authenticate(){
}
};
int main(){
Login LOGIN;
LOGIN.welcome();
LOGIN.authenticate();
return 0;
}
必须使用构造函数。类和函数是不同的。
class Login: public Student{
//bool a = true; Works fine
bool a;
Login() : a(true)
{
}
public:
void authenticate(){
}
};