![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Location: St. Andrews, Manitoba, Canada
Posts: 9
Rep Power: 0
![]() |
Function Calls and Errors!
Hey all yal, Im pretty new to programming in C++ so be patient with me.
I get these errors: --------------------Configuration: battle - Win32 Debug-------------------- Compiling... battle.cpp Linking... battle.obj : error LNK2001: unresolved external symbol "int __cdecl battle(char,int,int)" (?battle@@YAHDHH@Z) battle.obj : error LNK2001: unresolved external symbol "int __cdecl randomfoe(char,int,int)" (?randomfoe@@YAHDHH@Z) Debug/battle.exe : fatal error LNK1120: 2 unresolved externals Error executing link.exe. battle.exe - 3 error(s), 0 warning(s) With this code: #include <iostream.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
//Prototyping-------------
int battle(char,int,int);
int randomfoe(char,int,int);
//------------------------
int main()
{
char foename[15] = "";
int foelvl = 0;
int map = 1;
battle(foename[15],foelvl,map);
return 0;
}
//////////////////////////////////////////////////////////
int battle(char foename[],int foelvl,int map)
{
randomfoe(foename[15],foelvl,map);
cout << "You are fighting a LVL "<< foelvl <<" " <<foename <<endl;
return 0;
}
int randomfoe(char foename[],int foelvl,int map)
{
short randfoelvl = 0;
short randfoename = 0;
srand(time(NULL));
randfoename = rand() + 1 % (3 - 1 + 1);
randfoelvl = rand() + 1 % (100 -1 + 1);
//RANDOM ENEMY NAME-------------------------------
if (map = 1)
{
if (randfoename == 1)
foename = "Killer Mole";
if (randfoename == 2)
foename = "Crazed Bat";
if (randfoename == 3)
foename = "Giant Worm";
}
//RANDOM ENEMY LEVEL------------------------------
if (map = 1)
{
if (randfoelvl >=1 && randfoelvl <=30)
foelvl = 1;
if (randfoelvl >=31 && randfoelvl <=80)
foelvl = 2;
if (randfoelvl >=81 && randfoelvl <=100)
foelvl = 3;
}
return foelvl;
}WHY? I personally think it has to do with me calling the randomfoe function from the battle function. How do I fix it? Last edited by glopal; May 3rd, 2005 at 8:23 PM. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
change this:
battle(foename[15],foelvl,map); to this: battle(foename,foelvl,map); and change this: int battle(char,int,int); to this: int battle(char[], int, int); then do the same for the other function. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Location: St. Andrews, Manitoba, Canada
Posts: 9
Rep Power: 0
![]() |
Thx no more errors, but now I have a new problem.
When I call the randomfoe function from the battle function it doesnt seem to work. When I execute, I shows LVL 0, and nothing for the name. Help please! |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
C++ passes variables by value. If you don't know what this means, and whatever reference you're using to learn C++ doesn't explain it, you need to throw it away and get a new one.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
or in other words when passing a variable by value the original value of the variable does not change so when you pass a 2 to the function the variable when it returns from the funcition will still be 2.
This is why you have a return value from a function like you have it returning a int. Therefore you have to assign a variable to catch this int that is being returned(well you dont have to but its a good idea if returning somehting meaningful) so chenge it from randomfoe(foename[15],foelvl,map); to foelvl = randomfoe(foename[15],foelvl,map); jhope this helps a little |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Change char foename[15] in the function parameter declarations to char *foename, and char to char * in the prototypes at the top. Then pass foename, not foename[15] to the functions.
Last edited by Ooble; May 4th, 2005 at 11:12 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|