Could you actually post the code in question? According to msdn, LNK2001 means that you are referencing something that the linker can't find. Linker errors are hard to debug without any actual code, however :/
in the .cpp file of the header you have the class in just initialize it just like declaring a variable
ie:
1 2
int x = 5; //declares a variable and initializes it to 5
int Player1::level = 1; //should be in the .cpp file because you cant initialize static members inside the class
also...
start caring about neatness if you plan to make a career of programming, if an employer sees code like that they wont even give you an interview unless its absolutely genius code and even that thats hoping that they actually read it which is rare, learn neatness while you dont have a bunch of bad habbits and make a good one of it
I'm not sure if you can in this instance because you never actually create an object of Player1 therefore the constructor isn't called.
I suggest getting rid of the static members and creating an object of player1: Player1 player1(true);
Then access player1 members with the . operator instead of ::.
I understand that you are concerned about keeping Player1 in tact between function calls, so in an upper level function (int main?) define Player1 there and then pass the object by reference into void battlePhase() like so: void battlePhase(Player1 &player);
Another suggestion: Remove the bool Player1 argument from your constructor. I had to re-look at that one.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int damageDealt;
char name[10];
char Action;
class Boar1;
class Player1;
void battlePhase(Player1 &player, Boar1 &boar);
class Player1
{
public:
int health;
int exp;
int level;
int atk;
Player1 ()
{
health = 20;
level = 1;
exp = 0;
atk = 5;
damageDealt = atk;
}
};
class Boar1
{
public:
int health;
int level;
int atk;
Boar1 ()
{
health = 20;
level = 1;
atk = 5;
}
};
void battlePhase(Player1 &player, Boar1 &boar)
{
cout <<"You have run into a Boar!\nWhat do you want to do?\n1=Attack "<<endl;
cin >> Action;
if ( Action == '1' || Action == 'i' )
{
cout<<name<<" attacked enemy for " <<player.atk<< endl;
boar.health = boar.health - damageDealt;
if ( damageDealt < 0 )
{
damageDealt = 0;
cout<<"No damage!"<<endl;
}
elseif ( damageDealt == 0 )
cout <<"No damage!"<<endl;
elseif ( damageDealt > 0 )
cout << "enemy" << " took " << damageDealt << " damage!";
if(boar.health == 0)
{
cout<<"enemy has died!"<<endl;
cout<<"you have gained "<<"50"<<" exp!"<<endl;
system ("CLS");
player.exp += 50;
}
else
{
cout << "\nThat isn't an action! Try typing '1'.";
battlePhase(player,boar);
};
if(player.exp>=0 && player.exp<99) player.level=1;
if(player.exp>=100 && player.exp<199) player.level=2;
if(player.exp >=200 && player.exp <299) player.level=3;
if(player.exp>=300 && player.exp<399) player.level=4;
if(player.exp>=400 && player.exp<499) player.level=5;
if(player.exp>=500 && player.exp<999) player.level=6;
if(player.exp>=1000 && player.exp<1999) player.level=7;
cout<<"Congratulations, you are now level "<<player.level<<"!"<<endl;
system ("pause");
}
}
Oh by the way, I think you meant to have a } before line 73. That's one of the things that proper formatting will help you with. Also, instead of making battlePhase() recursive, just stick it in a while loop that will repeat as long as the action is invalid. Otherwise you'll get the "Congratulations, you are now level " as many times as you've called the function.
if i remove the static part, i get more errors.
i tried placing Player1 player1(true); at the top of my main.cpp, i got C2146 from it.
and i dont see why i have to do that 3rd one "void battlePhase(Player1 &player);"
and why do i need to remove the bool Player 1?
It has to be declared in a function. If you use the code I gave above, you will not get errors (except for a lack of a main). I was able to compile this without issue.
dont list to what stewbond said, hes wrong, what he said would be right if what you were talking about was a non-static member
but your variable is static, so you dont create it when you create an instance, its just like a global variable but you initialize it outside of the class