Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 传递结构错误“;'之前的不合格id=';代币;_C++_Structure - Fatal编程技术网

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);
}