C++ 传递结构错误“;'之前的不合格id=';代币;
我一直在尝试将一个包含所有变量的结构传递给多个函数,这些函数保存在一个单独的类中。我知道这个错误很可能与某种语法错误有关,但我不知道我做错了什么 主要内容如下:C++ 传递结构错误“;'之前的不合格id=';代币;,c++,structure,C++,Structure,我一直在尝试将一个包含所有变量的结构传递给多个函数,这些函数保存在一个单独的类中。我知道这个错误很可能与某种语法错误有关,但我不知道我做错了什么 主要内容如下: #include <iostream> #include <cstdlib> #include <ctime> #include <fstream> #include "running.h" using namespace std; int main() { //------C
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "running.h"
using namespace std;
int main()
{
//------Class Objects---------
running runObj;
//----------Vars--------------
char saveGame = 'N';
struct gameVar
{
int correctGuesses; // These vars need to be reset for each new game.
int Lives;
int rowCorrect;
int highScore;
char anotherGame;
} values;
values.highScore = 12;
values.anotherGame = 'Y';
//--------Game Loop-----------
// int highScore2 = runObj.readHighScore();
while (values.anotherGame = 'Y')
{
struct gameVar = runObj.processGame(gameVar);
struct gameVar = runObj.afterText(gameVar);
gameVar values;
values.anotherGame;
}
cout << endl << "-------------------------------------------------------" << endl;
cout << "Would you like to save your high score? Y/N" << endl;
cin >> saveGame;
if(saveGame == 'Y')
{
runObj.saveHighScore(gameVar);
}
return 0;
}
首先,一个简单的问题是:在
while
循环条件中使用=
,这将把值'Y'
分配给gameVar.anotherGame
。您实际需要的是==
,以测试是否相等
请看这一行:
struct gameVar = runObj.processGame(gameVar);
你想在这里干什么gameVar
是结构的名称,而不是gameVar
类型的对象。您的对象实际上被称为值
。也许你想做一些事情,比如:
values = runObj.processGame(values);
下一行也一样
您产生这种混乱的原因似乎是因为您在创建该类型的对象的同时定义了结构。名为gameVar
的struct
只是对象的蓝图,您创建的对象与名为值的蓝图相匹配:
struct gameVar
{
// ...
} values;
如果将main
函数之外的struct
定义为:
struct gameVar
{
// ...
};
然后在main
中使用以下命令创建它的实例:
gameVar values;
必须传递给函数的就是这个values
对象-不能传递类型,这就是gameVar
的含义
我不确定你当时想做什么:
gameVar values;
values.anotherGame;
这将重新定义循环中的值对象,而循环将在循环结束时销毁该对象。然后访问数据成员另一个游戏
,但不对其执行任何操作。也许你在寻找:
gameVar values;
values.highScore = 12;
values.anotherGame = 'Y';
while (values.anotherGame == 'Y')
{
values = runObj.processGame(values);
values = runObj.afterText(values);
}
值得注意的是,在C++中,在使用<代码> GAMEVAR < /C>类型之前,不需要将<代码>结构> /代码>。类型名称只是gameVar
。也就是说,您可以将processGame
的声明更改为:gameVar-processGame(gameVar)代码>谢谢您的帮助!我认为我现在对结构有了更坚定的理解,现在结构已经开始工作了。再次感谢你!
gameVar values;
values.highScore = 12;
values.anotherGame = 'Y';
while (values.anotherGame == 'Y')
{
values = runObj.processGame(values);
values = runObj.afterText(values);
}