9 Replies - 20138 Views - Last Post: 03 February 2010 - 12:59 PM Rate Topic: -----

#1 gcarlson53   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 27-January 10

"expected primary-expression before '&' token"

Posted 03 February 2010 - 11:59 AM

playerSlime(Player &player1, Monster &slime);


That is the line of code that I am getting the errors on. I am getting two of the same errors. One for each '&'
The error I am getting is...
"expected primary-expression before '&' token"

Does anyone know how to fix this?
Let me know if you need any more info.
Is This A Good Question/Topic? 0
  • +

Replies To: "expected primary-expression before '&' token"

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:00 PM

How are you using this?


Is that a prototype? Where's the return type on the function? Does that source have definitions for Players and Monsters?
Was This Post Helpful? 0
  • +
  • -

#3 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 547
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:04 PM

Looks like Player and Monster are not defined.
Was This Post Helpful? 1
  • +
  • -

#4 gcarlson53   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 27-January 10

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:34 PM

No that's not the prototype I was trying to call that function. I have two classes Player and Monster. My Player object is player1 and my Monster object is slime. here is more of the main file...
int main()
{
    Player player1;
    
    Monster slime;
    Monster bat;
    Monster hornet;
    Monster spider;
    Monster rat;
    Monster willowisp;
    Monster snake;
    Monster scorpion;
    Monster jellyfish;
    Monster plant;
    
    string inName;
    
    cout << "What is your characters name? ";
    getline(cin, inName);
    
    player1.setName(inName);    // Makes the users input the players name
    player1.setLevel(1);        // Sets the character's level to 1
    player1.setExperience(0);   // Sets the character's experience to 0
    player1.setHealth(650);     // Sets the character's health to 466
    player1.setStrength(17);    // Sets the character's strength to 17
    player1.setDefense(17);     // Sets the character's defense to 17
    player1.setAttackPower();   // Sets the character's attack power
    player1.playerDeath();      // Sets the character's death to false
    
    slime.setName("Slime");     // Sets the Slime's name
    slime.setLevel(1);          // Sets the slime's level to 1
    slime.setHealth(200);       // Sets the slime's health to 200
    slime.setStrength(18);      // Sets the slime's strength to 18
    slime.setDefense(6);        // Sets the slime's defense to 6
    slime.setAttackPower();     // Sets the slime's attack power
    slime.monsterDeath();       // Sets the slime's death to false
    
    
    
    playerSlime(Player &player1, Monster &slime);
    
    cout << endl;
    system("pause");
    return 0;
}



This is the function I'm trying to call...
void playerSlime(Player &player1, Monster &slime)
{
    int slimeHealth = slime.getHealth(); // Collect slime's health
    int attack = player1.getAttackPower(); // Determine strength of attack
    int defense = slime.getDefense();  // Collect slime's defense rating
    int damage = 0; // Initialize damage dealt by the attack

    

    damage = (1 + rand() % attack) - (1 + rand() % defense); // calculate damage dealt by blow
    
    if(damage > 0) // validate that the attack doesn't HEAL the opponent
    {
        cout << "\n" << player1.getName() << " dealt " << damage << " damage.";
        slimeHealth -= damage; // subtract attack from enemy's local health container
    } // end if
    else
    {
        // no damage dealt if the 'if' statement is not tripped
        cout << "\n" << player1.getName() << " did not deal any damage.";
    }
 
    if(slimeHealth < 1)     //--------------------------------------|
    {                       // So slime can't have negative health  |
        slimeHealth = 0;    //--------------------------------------|
    }
 
    slime.setHealth(slimeHealth); // Replace enemy's health with the adjusted value from local container
    cout << "\n" << slime.getName() << "'s remaining health is " << slime.getHealth() << endl;
    
    if(slimeHealth < 1)
    {
        cout << "The " << slime.getName() << " has died!\n";
    }
}


This post has been edited by gcarlson53: 03 February 2010 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:35 PM

playerSlime(player1, slime);

Was This Post Helpful? 2
  • +
  • -

#6 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 547
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:46 PM

@gcarlson53 the prototype says

void playerSlime(Player &player1, Monster &slime){



This tells the compiler that the two parameters are by reference (i.e. not as pointers). You therefore do not need the ampersand &, which the compiler takes as meaning 'address of' when calling this function. As NickDMax says, simply remove the & from the place where you call the function. So

playerSlime(Player &player1, Monster &slime);



becomes

playerSlime(Player player1, Monster slime);


This post has been edited by Martyn.Rae: 03 February 2010 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:54 PM

View PostMartyn.Rae, on 03 February 2010 - 12:46 PM, said:

playerSlime(Player player1, Monster slime);



You don't need the type when actually using the function, see Nick's post above.
Was This Post Helpful? 1
  • +
  • -

#8 gcarlson53   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 27-January 10

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:57 PM

Ohhhh yeahhhhh. I totally forgot that I was supposed to take that out. Thank you guys. You saved me a lot of frustration and confusion.
Was This Post Helpful? 0
  • +
  • -

#9 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 547
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:57 PM

Thanks KYA ... lost the plot there for a moment!
Was This Post Helpful? 0
  • +
  • -

#10 gcarlson53   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 27-January 10

Re: "expected primary-expression before '&' token"

Posted 03 February 2010 - 12:59 PM

Martyn.Rae I meant to say that your post was helpful but I accidentally pressed the minus sign so I hit the plus sign on your previous post. Sorry.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1