# Weird error when calling a function from a class

## Recommended Posts

hey all, i'm making a linked list here and have run into a problem: i have a soldier cpp where it contains the implementation of the soldier class from a header file. okay, heres the problem, when in main, i try and call a function from this class and it comes up with:
error C2352: 'Soldier::setRank' : illegal call of non-static member function

here is the set rank in the solder cpp file:
void Soldier::setRank(int totalKills)
{
if (totalKills >= 1000)
{
cout << "Your Rank is a Genreal.." << "\n";
m_Rank = "General";
}
else if (totalKills >= 500)
{
cout << "Your Rank is a Captain.." << "\n";
m_Rank = "Captain";
}
else if (totalKills >= 100)
{
cout << "Your Rank is a Soldier.." << "\n";
m_Rank = "Soldier";
}
else
{
cout << "Your Rank is Trainee.." << "\n";
m_Rank = "Trainee";
}

}

btw i get this error no matter what function i'm calling, but thought to include the implementation and calling of a function. here is how the function is called from main: [code] Soldier::setRank(tKills); [code] thankz 4 any help :) [Edited by - Twinsen2 on August 3, 2005 6:50:11 AM]

##### Share on other sites
soz bout repost.

[Edited by - Twinsen2 on August 3, 2005 6:12:55 AM]

##### Share on other sites
Looks to me that the setRank() function isn't declared as static within the Solider class.

To call a non-static function you need to instansiate an instance of the class and then use the objects setRank() function:

Soilder aSoldier;

aSolider.setRank();

OR

Soldier aSoldier = new Soldier();

aSoldier->setRank();

Hope this helps a little.

##### Share on other sites
Quote:
 Original post by garyfletcherLooks to me that the setRank() function isn't declared as static within the Solider class.To call a non-static function you need to instansiate an instance of the class and then use the objects setRank() function:Soilder aSoldier;aSolider.setRank();ORSoldier aSoldier = new Soldier();aSoldier->setRank();Hope this helps a little.

Man thankz alot yeah that worked 100%, i was just testing th setRank function at the top of the main file, i then looked at ur last peice of code, and thought....wtf did i put it there, i moved this to after the bit where a new soldier is created on the node and it worked fine.

Thankz alot dude.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627658
• Total Posts
2978484

• 10
• 12
• 22
• 13
• 33