Sign in to follow this  
Twinsen2

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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this