Sunday, 15 March 2015

c++ - Passing Struct Error "unqualified-id before '=' token" -



c++ - Passing Struct Error "unqualified-id before '=' token" -

i have been trying pass struct, hold vars, multiple functions, saved in separate class. know error has sort of syntax error, likely, not see have done wrong.

the main.ccp is:

#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 reset 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 save high score? y/n" << endl; cin >> savegame; if(savegame == 'y') { runobj.savehighscore(gamevar); } homecoming 0; }

my header file is:

#ifndef running_h #define running_h class running { public: struct gamevar processgame(struct gamevar); void savehighscore(struct hs); int readhighscore(); struct gamevar aftertext(struct gamevar); }; #endif // running_h

first of all, simple issue: using = in while loop condition, assign value 'y' gamevar.anothergame. want ==, test equality.

take @ line:

struct gamevar = runobj.processgame(gamevar);

what trying here? gamevar name of struct, not object of gamevar type. object called values. perhaps wanting like:

values = runobj.processgame(values);

ditto next line too.

it seems reason have confusion because you're defining struct @ same time creating object of type. struct called gamevar design objects , create object matches design called values:

struct gamevar { // ... } values;

you might less confused if define struct outside main function as:

struct gamevar { // ... };

and create instance of in main with:

gamevar values;

it values object must pass function - can't pass type, gamevar is.

i'm not sure attempting with:

gamevar values; values.anothergame;

this redefine values object within while loop , destroyed @ end of loop. access info fellow member anothergame don't it. maybe you're looking for:

gamevar values; values.highscore = 12; values.anothergame = 'y'; while (values.anothergame == 'y') { values = runobj.processgame(values); values = runobj.aftertext(values); }

it's worth noting in c++, not need set struct before every utilize of gamevar type. type name gamevar. is, alter declaration of processgame to: gamevar processgame(gamevar);

c++ structure

No comments:

Post a Comment