Weird error when calling a function from a class

Started by
2 comments, last by Twinsen2 18 years, 8 months ago
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: Soldier::setRank(tKills); thankz 4 any help :) [Edited by - Twinsen2 on August 3, 2005 6:50:11 AM]
Advertisement
soz bout repost.

[Edited by - Twinsen2 on August 3, 2005 6:12:55 AM]
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.
Gary.Goodbye, and thanks for all the fish.
Quote:Original post by garyfletcher
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.




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.

This topic is closed to new replies.

Advertisement