在主函数 c++ 中使用 类 时出现两个错误
two errors while using classes in main function c++
我在主函数中使用 类 时出现两个错误。
第一个错误-
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();
}
和"number"头文件-
#define SIZE 4
class number{
private:
public:
digit digitarray[SIZE];
number();
void numscan();
void randomnum();
int numreturn(int);
void numprint();
};
您的代码存在以下问题。
1) 您还没有创建对象并尝试访问 class 成员,这只能对静态 class 成员完成。
2) 不能显式调用析构函数。
修复很简单,声明一个类型为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
我在主函数中使用 类 时出现两个错误。
第一个错误-
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();
}
和"number"头文件-
#define SIZE 4
class number{
private:
public:
digit digitarray[SIZE];
number();
void numscan();
void randomnum();
int numreturn(int);
void numprint();
};
您的代码存在以下问题。
1) 您还没有创建对象并尝试访问 class 成员,这只能对静态 class 成员完成。
2) 不能显式调用析构函数。
修复很简单,声明一个类型为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