0

I have a variable in a main.cpp file like this:

SDL_Renderer* gRenderer = NULL;

and I have a class that is in separate files (a .h and a .cpp file). Inside the .cpp file I want to access gRenderer like this:

newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );

I have tried putting an SDL_Renderer inside the class but when I compile it gives me only one warning that it is unused and when I run the program I get a message from SDL_GetError() :

"Unable to create texture from colors.png! SDL Error: Invalid renderer"

How can I do that inside the class that is in the separate files?

Martin G
  • 17,357
  • 9
  • 82
  • 98
Shago
  • 101
  • 3
  • 9

1 Answers1

2

In order to access it, its file must be included in the file it will be used. Therefore, you should move the variable declaration to a header file (like main.h) and include it in main.ccp and in the files it will be used.

To do so, though, in the header file you should declare it as extern, and in main.cpp, defined normally:

main.h
extern SDL_Renderer* gRenderer;

main.cpp
SDL_Renderer* gRenderer = NULL;

That way, the variable is defined and can be used across multiple files normally, retaining its value.

If you want more information: How do I use extern to share variables between source files?

Community
  • 1
  • 1
Tarc
  • 370
  • 1
  • 7