代码之家  ›  专栏  ›  技术社区  ›  Akra

在main函数c中使用类时的两个错误++

  •  0
  • Akra  · 技术社区  · 10 年前

    在主函数中使用类时,我有两个错误。

    第一个错误-

    error C2227: left of '->digitarray' must point to class/struct/union/generic type
    

    第二个错误是-

    error C2675: unary '~' : 'game' does not define this operator or a conversion to a type acceptable to the predefined operator
    

    头文件-

    class game{
    private:
        int cows();
        int bulls();
        bool game_over = false;
    
    public:
        int x;
        number *user, *computer;
        game();
        ~game();
        game(const number,const number);
        void play();
    };
    

    主文件-

    int main(){
        game();
        for (int i = 0; i < SIZE; i++){
            cout << game::computer->digitarray[i].value;
        } 
    
        ~game();
    
    }
    

    和“数字”头文件-

    #define SIZE 4
    
    class number{
    private:
    public:
        digit digitarray[SIZE];
        number();
        void numscan();
        void randomnum();
        int numreturn(int);
        void numprint();
    };
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   πάντα ῥεῖ    10 年前

    修复非常简单,声明一个类型为 game :

    int main(){
        game g;
          // ^^
        for (int i = 0; i < SIZE; i++){
            cout << g.computer->digitarray[i].value;
                 // ^^
        } 
    
        // ~game(); <<< You don't need this or g.~game();
    }   // <<< That's done automatically here
    
        2
  •  0
  •   Ajay    10 年前

    您的代码有以下错误。

    1) 您尚未创建objected并尝试访问类成员,这只能对静态类成员执行。

    2) 不能显式调用析构函数。

    推荐文章