C++:How can I call a member function of an object through a function in a header file

Started by
6 comments, last by chrono1699 16 years, 4 months ago
Heres what im trying to figure out. How can you call a 'member function' of an object through a 'function' in a header file. This is the error that shows up when compiling: " 'Object' : undeclared identifier " main.cpp:

#include<iostream>
#include"CObject.h"

using namespace std;

     //create an object called 'Object' from the CLASS 'CObject' defined
     //in "CObject.h"
     
     CObject Object;

int main()
{
	//Call HeaderFileFunction() defined in "Cobject.h" to call the
        // member function GetNumberFromObject()
	
        HeaderFileFunction();
                                                            
return 0;
}

CObject.h:
 
#include<iostream>

using namespace std;


void HeaderFileFunction()
{
     //Call the objects member function
     Object.GetNumberFromObject();
}




//Create an object class called CObject
class CObject
{
public:
	
	///////constructor////////////////////////////
	CObject()
	{
		m_number = 1;
	}
	//////////////////////////////////////////////


		////// Member function were trying to call /////////
		void GetNumberFromObject()
		{
			cout << m_number;
		}
		////////////////////////////////////////////////////
		

private:

	/////// private data members ///////
	int m_number;
	////////////////////////////////////
};

[Edited by - Marquis DeBlodey on December 21, 2007 10:07:11 PM]
Advertisement
Your header file doesn't know about Object. I suggest you rewrite HeaderFileFunction like so:

void HeaderFileFunction(CObject argumentObject){//Call the objects member functionargumentObject.GetNumberFromObject();}


Then, when you call it, pass it the CObject who's method you want to run:

CObject objectFromMain;int main(){//Call HeaderFileFunction() defined in "Cobject.h" to call the// member function GetNumberFromObject()HeaderFileFunction(objectFromMain);return 0;}


See if this helps. (BTW, you can include source code in your posts by putting it between source tags, like: [ source]some code goes here[ /source]. Take out the spaces in the [], I included them so they'd show up.)
The function HeaderFileFunction is trying to use a CObject instance called Object but it does not know about Object.

If you are going to try and call a non member function and use an existing class object inside the function then it has to know about the instantiated object somehow. So try passing the object into the function like so:

void HeaderFileFunction(const CObject& Object){   //Call the objects member function   Object.GetNumberFromObject();}



then call the function from main() like this:

CObject Object;int main(){   HeaderFileFunction(Object);   return 0;}
Hey thanks for the fast reply guys. I posted like 10 minutes ago.

I tried what you guys posted but i get new errors.

CObject.h:

void HeaderFileFunction(const CObject& Object)   //<---{   //Call the objects member function   Object.GetNumberFromObject();}
.

for this i get this error on the first line:

missing type specifier - int assumed. Note: C++ does not support default-int
I dunno if im doing something wrong.
//figuring this out
That's because you use type CObject before declaring it. Put the declaration of the class before the function.
My Blog
Woohoo! it works! Hey thanks alot guys.
There's several ways accessing objects' private member variables. The easiest way is using accessor methods from that same class.

//player.h#pragma onceclass player{public:      void getHP();      void setHP(int hitpoints);private:      int m_HP;};void player::getHP() { int hitpoints = m_HP; cout << hitpoints << endl; }void player::setHP(int hitpoints) { m_HP = hitpoints; }


use it in the driver program
//main.cpp#include <iostream>#include "player.h"int main(){       player xyz;       xyz.setHP(10);       xyz.getHP();       xyz.setHP(30);       xyz.getHP();}


There's also a way in accessing one objects member variable with another objects member variable. For example, simulating a simple fighting game like so:

//main.cpp#include <cstdlib>#include <iostream>#include "monster.h"#include "player.h"using namespace std;int main(int argc, char *argv[]){    monster monst;	player xyz;	xyz.setHP(20);	monst.setHP(30);	cout << "monster's HP = " << monst.getHP() << endl;	cout << "players's HP = " << xyz.getHP()	<<	endl;	cout << "monster attacks" << endl;	monst.fight(xyz);	xyz.fight(monst);	cout << "monster's HP = " << monst.getHP() << endl;	cout << "player's HP = " << xyz.getHP() << endl;    system("PAUSE");    return EXIT_SUCCESS;}


//player.h#ifndef PLAYER_H#define PLAYER_Hclass monster;class player{      public:             void fight(monster& monster);			 void setHP(int hitpoints);			 int getHP();			 void takeDamage(int damage);      private:              int m_HP;};#endif


//monster.h#ifndef MONSTER_H#define MONSTER_Hclass player;class monster{      public:             void fight(player& player);			 void setHP(int hitpoints);			 int getHP();			 void takeDamage(int damage);      private:              int monster_HP;};#endif


//monster.cpp#include "monster.h"#include "player.h"void monster::takeDamage(int damage){	monster_HP -= damage;}int monster::getHP(){	return monster_HP;}void monster::setHP(int hitpoints){	monster_HP = hitpoints;}void monster::fight(player& player){	int damage = 1;	player.takeDamage(damage);}


//player.cpp#include "player.h"#include "monster.h"void player::takeDamage(int damage){	m_HP -= damage;}int player::getHP(){	return m_HP;}void player::setHP(int hitpoints){	m_HP = hitpoints;}void player::fight(monster& monster){	int damage = 1;	monster.takeDamage(damage);}

This topic is closed to new replies.

Advertisement