Making a c++ text game having issue with passing class information to function...

Started by
3 comments, last by VValkingman 15 years, 9 months ago
Hey, I am having some issues with passing information that my class collects to a function. It has been sometime since ive picked up c++, but I got bored and decided to make a text game. Id rather not post all 600 lines of code so here is where I believe the issue is. #include <iostream> #include <string> #include <windows.h> #include <cstdlib> #include <ctime> using namespace std; class Player { public: string playerName; string playerGenre; string playerRank; int health; int mana; int strength; int imagination; int paper; int knowledge; int determination; Player(int h, int m, int s, int p, int i, int k, int d); void move(); void attackMonster(); void getTreasure(); }; Player::Player(int h, int m, int s, int p, int i, int k, int d) { paper = p; imagination = i; knowledge = k; determination = d; health = h; mana = m; strength = s; } //Function Prototypes void newGame(); void continueGame(); void quit(); int battle(Player &); int statAllocate(int &, int &,int &, int &, int &, int &); //alot of space********************** Player test1(tempHealth, tempMana, tempStrength, charPaper, charImagination, charKnowledge, charDetermination); //alot more space******************** battle(test1); //me calling the battle function and trying to pass test1 //space**************************** int battle(Player test1) //battle function definition { //function code } any ideas? I keep getting error LNK2019 and LNK1120. If I need to post all of the code tell me, but im thinking that what i have up here is the problem *shrug* (maybe pointers? been so long since ive done that though) Thanks for the help
Advertisement
Those are linker errors. It's not a coding problem... yet. :] (j/k)
Make sure you have all necessary file included properly and everything is there.
Holy crap, you can read!
the first error will result if you use an external variable with out declaring the variable

so if you have something like this

extern int GameState;

with out delcaring

int GameState;

in another file you'll get the lnk2019 error

the other error lnk1120 is because it can't find a function symbole so you are probally missing a function declaration
0))))))>|FritzMar>
There could be many problems with your code, but the only problem with the code you actually posted is this:

int battle(Player &);(...)int battle(Player test1) //battle function definition{//function code}


You declare the function battle() to accept a reference to a Player object, then in the implementation make it accept a simple Player object. Since you're using C++ the linker is probably looking for an overloaded version of battle() that accepts a reference and, failing to find one, spitting out an error.

A simple & should fix the problem:

int battle(Player& test1) //battle function definition{//function code}
Yea that was it lol all i needed was battle(Player& test1), works like a charm now thanks!

This topic is closed to new replies.

Advertisement