Dynamic memory pointer instance used as function paramater?

Let's say I have a class pointer which I use for dynamic memory somewhere in a header file.

gameState *Gamestate = new gameState;

And I want to use that instance of the gamestate as a function paramater.

void changeGamestateOnClick(gameState & *GameState);

How can I do that?
What problem are you having trying to do that? It should work as you have it.

BTW, you should not be initializing the pointer in a header file. That initialization should be done in a .cpp file. Initializing the pointer in a header file can cause confusion if the header is included in multiple places.
closed account (zwA4jE8b)
1
2
3
4
5
void changeGamestateOnClick(gameState *GameState)
{
   //In here just dereference your pointer and set it to whatever it is you need
  *GameState = NEW GAME STATE
}


There is no need to send a pointer as a reference, at least in this case.

so in your code

1
2
3
4
int main()
{
   //blah blah
   changeGamestateOnClick(GameState);


GameState is already a pointer so just call the function like that
Last edited on
@CreativeMFS I did this:

void gameMenu::clickActiveLink(sf::RenderWindow &winMain, gameState *GameState)

And when I call this:

GameMenu -> clickActiveLink(winMain, GameState);

I get the following error:

GameMenu.obj : error LNK2019: unresolved external symbol "public: void __thiscall gameState::setGamestate(int)" (?setGamestate@gameState@@QAEXH@Z) referenced in function "public: void __thiscall gameMenu::clickActiveLink(class sf::RenderWindow &,class gameState *)" (?clickActiveLink@gameMenu@@QAEXAAVRenderWindow@sf@@PAVgameState@@@Z)
C:\Documents and Settings\Octav\Desktop\C++\Adventure Game\Council of Torment [CoT]\Debug\SFML Project.exe : fatal error LNK1120: 1 unresolved externals
Maybe the function you've called contains the function setGamestate, that says the function setGamestate has not had any code yet.
You defined the function but it seems you haven't written any code for the function setGameState.
closed account (zwA4jE8b)
Yes like Jackson Marie says, this is a link error. In your .h file you need the function prototype, then in your .cpp file you need the functin definition.

Also refferring to AbstrationAnon post,

in .h file
1
2
3
4
...
gameState *Gamestate;
void clickActiveLink(sf::RenderWindow &winMain, gameState *GameState);
...



in .cpp file
depending on where you define the Gamestate variable
1)static
or
2)in class (as member)
1
2
3
4
5
6
7
8
9
10
11
1) gameState::Gamestate = new gameState;
//or
2)
   //in class constructor 
  Gamestate = new gameState;

void gameMenu::clickActiveLink(sf::RenderWindow &winMain, gameState *GameState)
{
 //code here
}
 


you can post valid parts of your code just use the code tags
<>
Last edited on
The linker error is telling you the function
 
void gameState::setGamestate(int)

was declared in your class and was called from function
 
void gameMenu::clickActiveLink(class sf::RenderWindow &,class gameState *)

but you never implemented the function.

OK Let's settle what my setting is:

I have the following files:

gameState.h
gameState.cpp (setGamestate function is here)
gameMenu.h
gameMenu.cpp (function that requires the gamestate pointer is here)
game.cpp (the pointer gets created here, and the function gets used here)

Inside gamestate.h I define a gamestate class, with a setGameState public function.
Inside gameMenu.h I included gameState.h and I am trying to write a function that changes the gamestate (which uses dynamic memory)'s gamestate.

I took a close look at the paramaters and double checked everything, it's impossible to be a linker error, but what seems to be the problem is this line INSIDE the function that I'm trying to write:

<>GameState->setGamestate(RUNNING);<>

The function looks like this:

<>void gameMenu::clickActiveLink(sf::RenderWindow &winMain, gameState *GameState)<>
Last edited on
If you are using an IDE, Did you add "gameState.cpp" and "gameMenu.cpp" with the projects' files?
Can you show us the class declaration for gameState along with implementation of gameState::setGamestate?

Please use code tags which are
[code]
and
[/code]
, not <>.

I Found the problem, you were right, I did have the setGamestate function declared properly however for some reason I accidentally erased it, no idea how, but I re-wrote it and mostly everything works as intended.

Thanks everyone :)
Topic archived. No new replies allowed.