# Some weird errors in C++

## Recommended Posts

bballmitch    100
Here's my code:
class Environment
{
private:
int e_iCol;

int e_iNumAnts;

Ant *ants;
public:

Environment(int size)  //specify the size of the environment
{
e_iCol = size;
e_iNumAnts = 0;
ants = new Ant[1];
}

void draw()         //implement graphics here; don't forget to draw the environment as well
{
for(int i = 0; i < e_iNumAnts; i++)
drawAnt( ants[i].location() );
}

void drawAnt(Location loc)  //draws an ant at this loctaion
{
}

void addAnt(Ant &curr) //add this Ant to the Ants that are in this environment
{
e_iNumAnts++;

Ant *oldAnts = ants;
ants = new Ant[e_iNumAnts];
for(int i = 0; i < e_iNumAnts-1; i++)
ants[i] = oldAnts[i];
ants[e_iNumAnts-1] = curr;

delete [] oldAnts;
}

void removeAnt(Ant &curr)  //remove this ant if its in this environment
{
int pos = -1;

for(int i = 0; i < e_iNumAnts; i++)
if( ants[i] == curr )
{
pos = i;
break;
}

if( pos >= 0)
{
Ant oldAnts[] = ants;
ants = new Ant[e_iNumAnts];
for(int i = 0; i < oldAnts.length; i++)
if( i == pos )
break;
else
ants[i] = oldAnts[i];

for(int i = pos + 1; i < oldAnts.length; i++)
ants[i] = oldAnts[i];

}

delete curr;
}

int numRowsAndCols()  //size of one side of the environment
{
return e_iCol;
}

bool isValid(Location loc)  //check if this location is in the environment
{
if( (loc.numRow() <= e_iCol) && (loc.numCol() <= e_iCol) )
{
return true;
}
else
{
return false;
}
}

int numAnts()  //return the number of ants in the environment
{
return e_iNumAnts;
}

Ant antAt(Location loc)  //returns the Ant at that location
{                        //if there isn't an ant there, then it returns null
Ant a1 = null;

if( isValid(loc) )   //make sure the location is in the environment
{
for(int i = 0; i < e_iNumAnts; i++)
{
if( ants[i].location().equals(loc) )
{
a1 = ants[i];
break;
}
}
}

return a1;
}

void update()
{
for(int i = 0; i < e_iNumAnts; i++)
{
ants[i].act();
}
}
};


and I keep getting such weird errors like: 33 variable or field drawAnt' declared void 33 expected ;' before '(' token Thats so weird though. What is the problem with that line?

##### Share on other sites
bpoint    464
drawAnt is being used (in draw) before it is being declared.

Either prototype drawAnt before you use it, or (better) keep the code separate from the class declaration! Yech. :)

##### Share on other sites
I would tend to agree with bpoint

Code that is within the class block becomes unreadable and extremely hard to maintain (and debug for that matter) especially when you have a decent amount of code...

##### Share on other sites
_goat    804
Quote:
 Original post by bpointdrawAnt is being used (in draw) before it is being declared.Either prototype drawAnt before you use it, or (better) keep the code separate from the class declaration! Yech. :)

Er, no. Not a problem - methods in a class don't require an "order" of declaration. Unreadable, perhaps.

I can't figure it out - but make sure that the Location class is "seeable" from the Environment class.

##### Share on other sites
CTar    1134
Make sure Ant and Location is declared before Environment. Also what does location() return, I would expect an Location object, but it might be something else. Also can we see the declaration of Location? It doesn't have a private/protected copy constructor or something like that, right? Also is there a macro named location?

I can't think of anything else which would cause it.

##### Share on other sites
Ezbez    1164
Use std:vector or std::list(probably a better choice for this situation), not an array. You'll save yourself alot of time in the end.

Could drawAnt() be private? I don't think that you'll need it in the interface.

You're making the number of of ants be 0, but you've declared one ant.

But I don't really know what is wrong with your code. Would you mind posting all o f the errors? If you're getting alot of errors, just post a couple more. It may or may not help us.

##### Share on other sites
Quote:
 Original post by EzbezYou're making the number of of ants be 0, but you've declared one ant.

That's not a bug; his addAnt() function expects the ants array to be created because it deletes it without checking if it's been allocated or not. His code works fine.

There are no bugs in the code that's been posted. We need to see all the errors, particularly the first couple. Often one bad line of code can result in the rest of the file failing to compile correctly, and fixing that one line removes all other errors.

## 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