【问题标题】:This Declaration has no store class -> encounter problem while using bool in classes cpp problem此声明没有存储类 -> 在类 cpp 问题中使用 bool 时遇到问题
【发布时间】:2022-01-20 20:34:45
【问题描述】:

我在做一个大学项目,遇到了这个问题 -> 当我声明 bool a 并在其中存储值 true/false 时,代码运行良好 但是虽然我只声明并在之后存储值,就像在代码中一样,但它显示了一个错误,谁能解释它为什么......?

#include <iostream>

class Design{
    public:
    void welcome(){
        std :: cout << "------------------------------------------------------------\n";
        std :: cout << "  Welcome to Faculty Feedback form. Enter Your credentials\n";
        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;
}

【问题讨论】:

    标签: c++ oop


    【解决方案1】:

    您必须使用构造函数。类和函数不同。

    class Login: public Student{
        //bool a = true; Works fine
        bool a;
        Login() : a(true) 
        {
        }
        public:
        void authenticate(){
    
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 2012-04-04
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多