#include <iostream>
#include<string>
usingnamespace std;
void DealDamage(int &EnemyHealth, int DamageAmount, string EnemyName)
int main()
{
int EnemyHealth, DamageAmount;
DamageAmount = 50;
EnemyHealth = 100;
string EnemyName = "a rat";
cout << "Health before function call: " << EnemyHealth;
DealDamage(EnemyHealth, DamageAmount, EnemyName);
cout << "Health after function call: " << EnemyHealth << endl;
return 0;
}
void DealDamage(int &EnemyHealth, int DamageAmount, string EnemyName)
{
&EnemyHealth-=DamageAmount;
cout << "blade does " << DamageAmount << " damage to " << EnemyName << endl;
}
error: expected initializer before 'int'
In function 'void DealDamage(int&, int, std::string)':|
lvalue required as left operand of assignment|
||=== Build finished: 2 errors, 0 warnings ===|
When you pass by reference using a C++ Reference (notice the capitalization) as in your code, you don't need to add any extra syntax when you refer to the variable within the function. Its value gets automatically updated in the caller.
Also note that the variable isn't &EnemyHealth of type int, it's EnemyHealth of type int&.