please help, Game not working, again

Started by
1 comment, last by mbruenjes 18 years, 6 months ago
I have previously posted about a game where you raise a warrior and train it and fight it and i was having problems, various people worked with me to fix those problems but I have added some new functions and now it is compiling with errors again, So i was wondering if anyone could offer a helping hand. ERRORS C:\C++ Projects\rw.cpp In function `int main()': 48 C:\C++ Projects\rw.cpp expected primary-expression before '&' token 48 C:\C++ Projects\rw.cpp `warrior1' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.) C:\C++ Projects\rw.cpp In function `void displayGameMenu(warrior&)': 85 C:\C++ Projects\rw.cpp expected primary-expression before '&' token those are the errors I'm getting, and heres my source, oh and im using Dev-C++ Sorry, I know the code is a little messy.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class warrior
{
      public:
             warrior();
             string m_warriorName;
             int m_strength;
             int m_health;
             int m_defense;
             void fight();
             void trainHealth();
             void trainStrength();
             void trainDefense();
             
};

// warrior warrior1; // Take this out...

void newGame(warrior &warrior1);
void loadGame();
void createWarrior();
void displayIntro();
void displayGameMenu();
void displayCharacterInfo(warrior &warrior1); // Get to this...
void displayEquipMenu();
void displayItemMenu();
void displayArenaMenu();

int main()
{
    char menuChoice;
    menuChoice = 2;
    
    cout << "\t\t\t\t";
    cout << "Raise a Warrior" << endl << endl;
    
    cout << "1. New Game" << endl;
    cout << "2. Load Game" << endl;
    
    cin >> menuChoice;
    
    if ( menuChoice == '1')
    {
         newGame(warrior &warrior1);
    }
    else if ( menuChoice == '2')
    {
         loadGame();
    }
    else
    {
        cout << "Invalid choice, program terminating!" << endl;
    }
    
    system("pause");
    
    return 0;
}

void displayIntro()
{
     cout << "Welcome to Raise a Warror. In this game you will purchase a ";
     cout << "slave and train him to be the best fighter in the land so you ";
     cout << "can be wealthy!" << endl << endl;
     system("pause");
}

void displayGameMenu(warrior &warrior1)
{
     int gameMenuChoice;
     
     system("cls");
     cout << "1. Character Info" << endl;
     cout << "2. Equip" << endl;
     cout << "3. Item" << endl;
     cout << "4. Arena" << endl;
     cout << "5. Save Game" << endl;
     
     if ( gameMenuChoice == 1 )
     {
          displayCharacterInfo(warrior &warrior1);
     }
     else if ( gameMenuChoice == 2 )
     {
            displayEquipMenu();
     }
     else if ( gameMenuChoice == 3 )
     {
            displayItemMenu();
     }
     else if ( gameMenuChoice == 4 )
     {
            displayArenaMenu();
     }
     else if ( gameMenuChoice == 5 )
     {
            cout << "YOU CANT SAVE THIS GAME SILLY!";
     }
     else
     {
         displayGameMenu();
     }
     
}

void displayEquipMenu()
{
     system("cls");
     
     cout << "EQUIPMENT LIST GOES HERE!";
     
     displayGameMenu();
}

void displayItemMenu()
{
     system("cls");
     
     cout << "ITEM LIST GOES HERE!";
     
     displayGameMenu();
}

void displayArenaMenu()
{
     system("cls");
     
     cout << "ARENA MENU HERE";
     
     displayGameMenu();
}

void newGame()
{
     warrior warrior1;  // This is where you need the warrior, right?
     
     system("cls");
     displayIntro(); // Doesnt need it, right?
     createWarrior(); // Clears the screen so far, so it doesnt need it either...
     cout << endl << endl;

     displayGameMenu(); // This doesnt need your warrior, either...
}

void loadGame()
{
     cout << "Which game file would you like to load?" << endl;
}




void createWarrior()
{
     system("cls");

}

void displayCharacterInfo(warrior &warrior1)
{
     // So far, your not using this function...
     // But lets say you use it and you really want to
     // display those stats...
     // warrior1 is declared in newGame(), so this function needs a
     // a copy of that warrior warrior1...
     // to do that, pass it in...  As above^^^^^
     // When we want to call and display the stats,
     // displayCharacterInfo(warrior1);
     // as long as warrior1 is declared wherever you are
     // calling that.  The "&" is for making it a reference...
     // You can leave it out, but leaving it out would mean this
     // function get a COPY of that variable.
     // The "&" lets it use the variable directly,
     // instead of copying it and geting one copy.
     // But be careful, cuz if you are passing in a reference,
     // you can change the variable accidentally.
     // I used the "&" to use that variable directly,
     // cuz it boosts performance.
     
     cout << "Warrior Name: " << warrior1.m_warriorName << endl;
     cout << "Health: " << warrior1.m_health << endl;
     cout << "Strength: " << warrior1.m_strength << endl;
     cout << "Defense: " << warrior1.m_defense << endl;
}

warrior::warrior()
{
                         int random;
                         int randomStat;
                         int randomHealth;
                         random = rand();
                         randomStat = ((random%5) + 1);
                         randomHealth = ((random%3) + 20);
                         
                         cout << "Warrior name? ";
                         cin >> m_warriorName;
                         m_strength = randomStat;
                         m_health = randomHealth;
                         m_defense = randomStat;
}

void warrior::fight()
{
     cout << m_warriorName << endl;
     cout << "HP: " << m_health << endl;
     cout << "STR: " << m_strength << endl;
     cout << "DEF: " << m_defense << endl;
     cout << "---------------------" << endl;
}

void warrior::trainHealth()
{
     cout << "Current Health: " << m_health << endl;
     cout << "Cost to train: " << m_health*30 << endl;
     cout << "";
}

void warrior::trainStrength()
{
     cout << "Current Strength: " << m_strength << endl;
     cout << "Cost to train: " << m_strength*30 << endl;
}

void warrior::trainDefense()
{
     cout << "Current Defense: " << m_defense << endl;
     cout << "Cost to train: " << m_defense*30 << endl;
}


Advertisement
First message:
C:\C++ Projects\rw.cpp In function `int main()':
Indicates the first error is in main.

Next message:
48 C:\C++ Projects\rw.cpp expected primary-expression before '&' token
Something is wrong before a & sign on line 48. Line 48 looks like this:
newGame(warrior &warrior1);

Sorry thought you needed a comma, but you just need to remove the first word(see EDIT2) Next message(on line 48) will probably disappear when you correct the first.

EDIT:
Then in function "void displayGameMenu(warrior&)" you have an error on line 85.
displayCharacterInfo(warrior &warrior1);

Again something wrong before a & sign, again you have prefixed the type.

EDIT2:
It looks like what you were doing were actually to prefix the parameters with their type, you dont have to do that. If you have a function like this:
void Foo(A a); // A == Custom class
You can call it like this:
A AObj;
Foo(AObj);
But this will give errors:
A AObj;
Foo(A AObj);
Maybe you get it, but you have

void displayGameMenu(warrior &warrior1)
{
int gameMenuChoice;

and so on and declared a function named

void displayGameMenu();

Oh, and another thing:

newGame(warrior &warrior1);

Delete warrior or say newGame(new warrior);

The same here:

if ( gameMenuChoice == 1 )
{
displayCharacterInfo(warrior &warrior1);
}

in your displayGameMenu-Function.
Just use displayCharacterInfo(&warrior1);

The last thing:

If you create your warrior-object in your newGame-Function, this object can just used within your function. Either you pass your object as a reference parameter to every function, or you create a global object (what you have done before).

This topic is closed to new replies.

Advertisement