# another member function prob post...

This topic is 4533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok i have read all the other post related to member functions and have tried many different methods of solving my problem.... here is the general setup of the issue.. ill be concise, so i have leftout the obvious parts that would be required to make this a functional program

/////////////////////////////////
////////    MAIN   /////////////
class Game
{
CConsole * myConsole

Game()
{
myConsole = new CConsole;
myConsole->init(this);
}
~Game()
{
delete myConsole;
}
int consoleParser( CCommands* pCommand ){};
}
winMain(){myGame* thisGame}
/////////////////////////////////////////////////////
/// THESE CLASSES AREIN THERE OWN FILES  .h .cpp
/////////////////////////////////
//////// CCommands /////////////
class CCommands{}
/////////////////////////////////
//////// CConsole /////////////
class Game  // for external reference
class CConsole
{
Game * m_pGame;
// member function pointer
int Game::*funcPtr(CCommands*);
//
void init(Game * pGame)
{
m_pGame = pGame;
/////////////////////////////////////////////////////////////
funcPtr = &Game::consoleParser; // this is the problem line
// Ive tried ------------------------------
// funcPrt = Game::consoleParser          |
// funcPtr = m_pGame->*consoleParser      |
//// others too                           |
// ----------------------------------------
}
}


if i comment out that one line it compiles fine I get these errors when i compile my code with the problem line error C2276: '&' : illegal operation on bound member function expression error C2027: use of undefined type 'Game': see declaration of 'Game' ^^^^^ this error does not make sense to me.. because the function with the problem line of code accepts a GAME object and compiles .. as long as the "bad line" is x'ed out... i really need some advice on this.... thanks

##### Share on other sites
Uhm.. When you say that the classes are in their own files, you do mean that you've split the class definition (.h) from its implementation (.cpp) right?

Can you perhaps repost your code with the contents of each file separately? Your problem is pretty easy to fix, but it'd be easier to explain once we know what your current layout looks like.

##### Share on other sites
Quote:
 Original post by MTclipOk i have read all the other post related to member functions and have tried many different methods of solving my problem....

Well aside from the fact that the source doesn't compile the problem with the function pointer is due to how it is declared. Use the following:

	typedef int (Game::*FUNCPTR)(CCommands*);	FUNCPTR funcPtr;	// use the following	funcPtr = Game::consoleParser; // this is the problem line

##### Share on other sites
not a prob here

// MAIN.CPPclass Game{	CConsole * myConsole	Game()	{		myConsole = new CConsole;		myConsole->init(this);	}	~Game()	{		delete myConsole;	}	int consoleParser( CCommands* pCommand ){};}winMain(){myGame* thisGame}

//////// CCommands.h /////////////class CCommands{}/// the .cpp is not important to the prob// this is just to show that there is this class

///////////////////////////////////////// CConsole .h /////////////class Game  // for external referenceclass CConsole{	Game * m_pGame;	 // member function pointer	int Game::*funcPtr(CCommands*);//	void init(Game * pGame);}

///////////////////////////////////////// CConsole.cpp /////////////class Game  // for external referencevoid CConsole::init(Game * pGame){	m_pGame = pGame;	/////////////////////////////////////////////////////////////	funcPtr = &Game::consoleParser; // this is the problem line	        // Ive tried ------------------------------	// funcPrt = Game::consoleParser          |	// funcPtr = m_pGame->*consoleParser      |	//// others too                           |	// ----------------------------------------        // any time i try to use Game:: or m_pGame  -- error }

thats all of the part that causes or is related to the error...

##### Share on other sites
You appear to be mis-using your forward declaration of Game everwhere. When the compiler sees a forward declaration, it only knows that the Game class exists somewhere - it does not know its definition, and thus until it has been defined, you cannot invoke nor refer to any of its members.

I started to rewrite your code so that it would work, but then I began to cry.

First of all, you need to learn how to be more consistant. Why do each of your class names begin with a 'C' except for Game?

Next, you need to define each of your classes in their own header files, as an example:

Game.h
#pragma onceclass Game{	int m_value;public:	Game();	~Game();	void Method();};

You then need to provide the implementation of your class in its own source file:

Game.cpp
#include "Game.h" // Tell the compiler where the class definition isGame::Game(){}Game::~Game(){}void Game::Method(){}

This makes everyone's life easier. Now, in Game.h, include only the headers that the class definition requires, and in Game.cpp, include only the headers that the class implementation requires.

Forward declarations are only used in rare cases where circular dependencies exist.

Surely there's an article about this somewhere.. Sorry for coming off so blunt, but until you understand these practices, you're only going to keep running into problems.

EDIT: In reference to your specific problem, CConsole.h needs to know about Game's definition before it can do anything like "int Game::*funcPtr(CCommands*);" You have to remove your forward declaration of Game, and replace it with #include "Game.h". Get rid of the forward declaration of Game in CConsole.cpp too while you're at it, I have no idea why it's there in the first place.

Game.h needs to know about CConsole, so here you would need a forward declaration. But in Game.cpp, include CConsole.h.

##### Share on other sites
haha.. sorry to cause you to cry...

one simple question... is there a difference between

#pragma once

class Game
{
int m_value;

public:

Game();
~Game();

void Method();
};

////////////////////
and
////////////////////

#ifndef
#define console_

class Game
{
int m_value;

public:

Game();
~Game();

void Method();
};

#endif

##### Share on other sites
Not really, no, however the #pragma directive is compiler specific. If you're using one of Microsoft's compilers, you can be sure that #pragma is supported, otherwise you might has well stick to your #define include guards.

Also, if you decide to use #define, you should make up "nicer" names:

Game.h:

#ifndef _GAME_H_#define _GAME_H_#endif

##### Share on other sites
well i figured this out finally...

had to have the forward declaration of class CGame in the console.h
and in the console.cpp had to have the #include"Game.h"

would not work any other way ... but now its perfect
thanks for all the suggestions

##### Share on other sites

This topic is 4533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628707
• Total Posts
2984313

• 23
• 11
• 9
• 13
• 14