C++ 在主函数c++;
在主函数中使用类时出现两个错误 第一个错误-C++ 在主函数c++;,c++,class,main,C++,Class,Main,在主函数中使用类时出现两个错误 第一个错误- 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 头文件- 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();
};
#define SIZE 4
class number{
private:
public:
digit digitarray[SIZE];
number();
void numscan();
void randomnum();
int numreturn(int);
void numprint();
};
主文件-
int main(){
game();
for (int i = 0; i < SIZE; i++){
cout << game::computer->digitarray[i].value;
}
~game();
}
您的代码有以下错误 1) 您尚未创建objected并尝试访问类成员,这只能对静态类成员执行
2) 不能显式调用析构函数 您的代码有以下错误 1) 您尚未创建objected并尝试访问类成员,这只能对静态类成员执行
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
intmain(){
游戏g;
// ^^
对于(int i=0;i //~game();修复非常简单,请声明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
intmain(){
游戏g;
// ^^
对于(int i=0;i //~game();不,你自己不调用析构函数!而且使用独立构造函数调用也是错误的。只需声明一个game
类型的变量。计算机变量是非静态成员变量。这些变量只能使用对象访问。而且析构函数不应显式调用。哦,该死,我实际上没有声明游戏变量riable…关于析构函数,我该怎么称呼它?@Akra“关于析构函数,我该怎么称呼它?”不需要。如果变量超出范围,编译器会自动执行此操作。不,您自己不调用析构函数!并且使用独立构造函数调用也是错误的。只需声明game
类型的变量。计算机变量是非静态成员变量。此类变量只能使用对象访问。并且e析构函数不应该被显式调用。哦,该死的,我实际上没有声明一个游戏变量…关于析构函数,我该如何调用它呢?@Akra“关于析构函数,我该如何调用它呢?”你不需要这样做。如果变量超出范围,编译器会自动这样做。2)-实际上你可以:)2)-实际上你可以:)