Sign in to follow this  
VValkingman

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

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this