error LNK2001

closed account (LAfSLyTq)
1>------ Build started: Project: Game1, Configuration: Debug Win32 ------
1> Main.cpp
1>Main.obj : error LNK2001: unresolved external symbol "public: static int Player1::level" (?level@Player1@@2HA)
1>Main.obj : error LNK2001: unresolved external symbol "public: static int Player1::exp" (?exp@Player1@@2HA)
1>Main.obj : error LNK2001: unresolved external symbol "public: static int Boar1::health" (?health@Boar1@@2HA)
1>Main.obj : error LNK2001: unresolved external symbol "public: static int Player1::atk" (?atk@Player1@@2HA)
1>Main.obj : error LNK2001: unresolved external symbol "public: static int Player1::health" (?health@Player1@@2HA)
1>Main.obj : error LNK2001: unresolved external symbol "public: static int Boar1::atk" (?atk@Boar1@@2HA)
1>Main.obj : error LNK2001: unresolved external symbol "public: static int Boar1::level" (?level@Boar1@@2HA)
1>C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\Game1\Debug\A Warrior Game.exe : fatal error LNK1120: 7 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========







this happened after i fixed alot of errors.. i dont know how to fix this one.
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 :/
is level , exp , health , atk function defined ... ?
closed account (LAfSLyTq)
sorry the code is so sloppy, i never really cared about neatness.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

	int damageDealt;
	char name[10];
	char Action;

class Boar1;
class Player1;
void battlePhase();
	class Player1
	{
public:

	static int health;
	static int exp;
	static int level;
	static int atk;
	Player1 (bool Player1)
	{
		health = 20;
		level = 1;
		exp = 0;
		atk = 5;
		damageDealt = atk;
	}
	};

	class Boar1
	{
	public:

	static int health;
	static int level;
	static int atk;
	Boar1 (bool Boar1)
{
		health = 20;
		level = 1;
		atk = 5;
	}
	};

	void battlePhase()
{
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 " <<Player1::atk<< endl;
Boar1::health = Boar1::health - damageDealt;
if ( damageDealt < 0 )
{
damageDealt = 0;
cout<<"No damage!"<<endl;
}
if ( damageDealt == 0 )
{
cout <<"No damage!"<<endl;
}
if ( damageDealt > 0 )
{
cout << "enemy" << " took " << damageDealt << " damage!";
}
if(Boar1::health == 0)
{
	cout<<"enemy has died!"<<endl;
	cout<<"you have gained "<<"50"<<" exp!"<<endl;
	system ("CLS");
	Player1::exp += 50;
}
else
{
cout << "\nThat isn't an action! Try typing '1'.";
battlePhase();
};
           if(Player1::exp>=0 && Player1::exp<99)
		   {
							Player1::level=1;
		   			   cout<<"Congratulations, you are now level "<<Player1::level<<"!"<<endl;
};
           if(Player1::exp>=100 && Player1::exp<199)
		   {
			                 Player1::level=2;
		   			   cout<<"Congratulations, you are now level "<<Player1::level<<"!"<<endl;
};
		    if(Player1::exp >=200 && Player1::exp <299)
		   {
			                 Player1::level=3;
		   			   cout<<"Congratulations, you are now level "<<Player1::level<<"!"<<endl;
};
            if(Player1::exp>=300 && Player1::exp<399)
		   {
			                 Player1::level=4;
		   			   cout<<"Congratulations, you are now level "<<Player1::level<<"!"<<endl;
};
            if(Player1::exp>=400 && Player1::exp<499)
		   {
			                 Player1::level=5;
		   			   cout<<"Congratulations, you are now level "<<Player1::level<<"!"<<endl;
};
            if(Player1::exp>=500 && Player1::exp<999)
		   {
			                 Player1::level=6;
		   			   cout<<"Congratulations, you are now level "<<Player1::level<<"!"<<endl;
};
            if(Player1::exp>=1000 && Player1::exp<1999)
		   {
			                 Player1::level=7;
		   			   cout<<"Congratulations, you are now level "<<Player1::level<<"!"<<endl;
}
	system ("pause");
}
	};


This isnt all the code, but this is all the code i have with the errors in it as far as i know.. i just dont know how to solve the error.
closed account (LAfSLyTq)
bump.
initialize the static variable ...
closed account (LAfSLyTq)
how do you do that?
closed account (LAfSLyTq)
bump.
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
Last edited on
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.
I think I have a disease. Whenever I see untidy code I have an urge to clean it. It must be like a digital OCD:

Here it is, fixed and tidied:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace 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;
        }
        else if ( damageDealt == 0 )
            cout <<"No damage!"<<endl;
        else if ( 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.
Last edited on
closed account (LAfSLyTq)
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.
closed account (LAfSLyTq)
thank you very much, but now i am back at the nonStatic member reference error.
closed account (LAfSLyTq)
bump
^Stop doing that every hour, it's absurd. Believe it or not people can actually see more than just the top post.
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
Topic archived. No new replies allowed.