• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help: C++ Unresolved Externals

Dudd

Platinum Member
I'm taking AP Computer Science, and I'm having a problem with one of the assignments for the class. My problem involves two files, randgen.h and aquafish.cpp. Randgen is a random number generator. You input the parameters, and it spits out a random number. Aquafish is a primitive simulation of a fish's swim patterns. The fish is on what is basically a number line, and each call to the function Swim () moves the fish one place in either direction. This is done with a call to randgen.h, specifically RandInt, and returns either 1 or 0. Based on which one is returned, the fish swims in either direction. My problem is that while this is linked, I get an LNK2001 error: unresolved externals. First off, what does this mean? I've seen this error before and been able to fix it, but I've never really found out what it means. Secondly, based on the code below, what have I done to trigger this?

First: Randgen.h

// To construct random integers in a given range, client programs
// should use RandInt. To construct random doubles, client programs
// should use RandReal. The ranges for the return values for these
// functions are indicated with mathematical notation:
// [0..max) means a number between 0 and max,
// including 0 but not including max;
// [0..max] means a number between 0 and max, including
// both 0 and max.
//
// For example,
// RandGen r;
// r.RandInt(5) an int between 0 and 5, including 0 but not 5,
// i.e., in [0, 5)
// r.RandInt(2, 5) an int between 2 and 5, including 2 and 5,
// i.e., in [2, 5]
// r.RandReal() a double between 0.0 and 1.0, including 0.0 but not 1.0,
// i.e., in [0.0, 1.0)
// r.RandReal(4.2,6.7) a double between 4.2 and 6.7, including 4.2 but not 6.7,
// i.e., in [4.2, 6.7)

Next: Aquafish.cpp, specifically the Swim () function where the randgen class is called

void AquaFish::Swim()
{
RandGen r;
int flip, sum, temp; //temp corrects error where the first random int is always the same
temp = r.RandInt (10); //takes first random int

for (int x; x<myTankSize; x++)
sum=sum + myIndex [x];
if (sum==0)
myIndex [myPosition]=1;

if (myPosition == myTankSize - 1)
{
myPosition--;
}
else if (myPosition == 0)
{
myPosition++;
}
else
{
flip = r.RandInt(2);

if (flip == 0)
{
myPosition++;
}
else
{
myPosition--;
}
}

if (myDebugging)
{
cout << "*** Position = " << myPosition << endl;
}

if (myPosition == 0 || myPosition == myTankSize - 1)
{
myBumpCount++;
}

myIndex [myPosition]= myIndex[myPosition] + 1;
}

There it is. I've really modified very little of the College Board's code, but I can't figure out what exactly I've done. Thanks for your help.
 
IIRC, unresolved externals either means that you have a variable declared as external (a form of a global declaration), but it is never defined anywhere in another file, or you have a static class member that is never declared (not sure, that may give you a different error).

Does the 'extern' keyword appear anywhere in your source?

 
Another thing that you should be aware of is that if you're compiling under MS Visual C++, all the files that you mentioned should be added to the project's workspace.


🙂atwl
 
Back
Top