• Advertisement

Archived

This topic is now archived and is closed to further replies.

first rpg (text)

This topic is 6002 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I stared working on a text rpg for practice and I get 2 errors and I can''t figure out why... this is what I get when I compile 1.obj : error LNK2001: unresolved external symbol "int __cdecl battle(void)" (?battle@@YAHXZ) Debug/nw text rpg.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. I''ll post the source code if necassary

Share this post


Link to post
Share on other sites
Advertisement
Are you including the right header files?

"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden

Share this post


Link to post
Share on other sites
I suppose take a look at this it''s not very organised but it''s my first program
I proly didn''t include the right header files but I dunno which ones to include

#include
int monsterhp = 10;
int herohp = 50;
int totalherohop = 50;
int choice = 0;
int damage = 5;
int potion = 3;
int potionuse;
int itemchoice = 0;
int battle();
void menu();
int itemamount();
int battle(int monsterhp, int herohp, int choice, int damage)
{

if (herohp==0)
{
cout<<" \n your dead \n" ;
}
if (herohp>0)
{
cout<<"\n A monster appears, what will you do? \n";
menu();
cin>>choice;
switch(choice)
{
case 1: cout<<"You attacked monster and did " < monsterhp = monsterhp - damage;
break;
case 2: itemamount();
break;
case 3: cout<<"\n you cant run \n";
break;
default : cout<<" \n Invalid entry \n";
break;
}
}


return monsterhp,herohp;
}
int itemamount()
{

cout<<" 1. Potion(s): " << potion << endl << "\n 2.cancel \n";
cout<< "what item will you use?";
cin>>itemchoice;
switch(itemchoice)
{ case 1 : cout<<" \n potion used 20 hp restored";
if(herohp < 30)
{
herohp = herohp + 20;
}
if(herohp > 30)
{
potionuse = 50 - herohp;
herohp = herohp + potionuse;
}
battle();
case 2 : battle();
}
return potion;
}
void menu()
{
cout<<"\n 1. Attack \n 2. item \n 3. run" << endl;
}
void main()
{
battle();
}

Share this post


Link to post
Share on other sites
I''m going to peice it together when I can get it to compile

Share this post


Link to post
Share on other sites
You have not defined the method int battle(), instead you have int battle(int, int, int, int).
You have to pass those parameters because the method battle() is considered to be different than battle(int, int, int, int).

"If consquences dictate our course of action, it doesn''t matter what''s right, it''s only wrong if you get caught."
- Tool

Share this post


Link to post
Share on other sites
You declare a function
int battle(); 

which is equivalent to
int battle( void ); 

and you call it in several places, but you never define it (fill out its body). You instead define a function
int battle(int monsterhp, int herohp, int choice, int damage) 

which is not the same thing. The compiler is simply telling you it can''t find the first function ( int battle( void ) ).

Also, MSVC (which I assume you''re using) outputs an error code with each line. Place your insertion carat there and hit F1 (Help). It''ll bring up the MSDN page on that error with an example of the error. It''s very useful when you''re learning the compiler.

Share this post


Link to post
Share on other sites
wierd
when I posted it, for some reason chunks of my source are gone

is gone and part of the case 1 is missing

Share this post


Link to post
Share on other sites
can someone ecplain that in a simpler form...I dont think I understand

Share this post


Link to post
Share on other sites
int battle();

right there you declared a function prototype.


int battle()
{
hp = hp - damage;
return 0;
}

is the definition.

and int battle(); is how you call your function.

now that is trying to read the variable damage, and hp. Damage and hp are variables in int main(), and they need to be passed as parameters to your battel function, so instead:

int battle(int hp, int damage);
as the prototype.

int battle()
{
hp = hp - damage;
return 0;
}

stays the same.

and when you call it this time:

int battle(100, 9);

that would take 9 away from 100.

"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden

Share this post


Link to post
Share on other sites
C and C++ encourage function prototyping: telling the compiler what the function will look like before you actually code it, so the compiler can check argument number and types (it''s basically to help you out).

So, when you typed the line
int battle(); 

the compiler treated it as a declaration (a prototype) and expected a definition (body). When you placed the statement
battle(); 

inside your switch statement and somewhere else, the compiler correctly assumed you were trying to call int battle(void) (the ''void'' means no arguments).

Since your variables are global to the program file, you could just change the line
int battle(int monsterhp, int herohp, int choice, int damage) 

with
int battle() 

It''ll work. Trust me.

What wont work is where you try to return 2 values:
return monsterhp,herohp; 

C/C++ do not support the return of tuples (multiple values). And you don''t need it anyway.

Review functions and global variables. Your program, as it is currently written, doesn''t need to pass any arguments to the battle() function - and it doesn''t.

Share this post


Link to post
Share on other sites
quote:
Original post by Drizzt DoUrden
Damage and hp are variables in int main()


Uh, if you''d read the code you''d notice that all main() contains is a call to battle(). damage and hp are declared and defined outside the main() function but within the implementation file.

The secret to giving good advice is listening.

Share this post


Link to post
Share on other sites
hmmm can you please tell me how much C++ do you know?

i also would like to know if you would like to join me in making some game i had in mind

my email is SonShadowCat@aol.com

Share this post


Link to post
Share on other sites
Oluseyi, I didnt even read his code. I was explaining how function worked. And, I made an error when calling the function too.

when you call it, you dont say:

int battle();

you just say:

battle();

"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden

Share this post


Link to post
Share on other sites
This is like my third program in c++(I mae a hello world program and an add 2 numbers prog)
I just did it to try to figure out functions ''cause the book I have doesn''t explain it very well

...sorry it sux so bad....

Share this post


Link to post
Share on other sites

  • Advertisement