Saturday, May 22, 2010

Weird microsoft visual basic c++ build error?

Program


#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;


#include %26lt;time.h%26gt;


using namespace std;


float rnd (float range);


int main ()


{


int num1;


cout %26lt;%26lt; "This program outputs random equations.\n";


system ("PAUSE");


num1 = rnd (300);


cout %26lt;%26lt; num1;


system ("PAUSE");


}

















int rnd ( int range)


{


srand ((unsigned)time(NULL)); //make seed from time


int r; //rand parameters and crearation


r = rand() % range; //uses seed without command


return (r);


}


errors


random_equations2.obj : error LNK2001: unresolved external symbol "float __cdecl rnd(float)" (?rnd@@YAMM@Z)

Weird microsoft visual basic c++ build error?
You've declared your rnd function as something that returns a float and then defined it as returning an int.





When you call rnd in main(), you haven't defined it yet (that happens further down), so main() will try to use the declared version (expecting a float and assuming that its an external function).





The linker fails because it can't find a rnd function which returns a float as main() expects.
Reply:You declared rnd as a function that expected a float parameter on Line 5.





But then you defined it later (below the main function) as having a parameter of type int. So your program is looking for a function that doesn't exist (it looks for a function with the same name, parameter types and return type). That's what undefined means; it can't find what it was told to look for.


No comments:

Post a Comment