I have two classes, and this is the header of one of them:
#ifndef WRAPPER_HPP
#define WRAPPER_HPP
#include <SDL/SDL.h>
using namespace std;
class Wrapper
{
private:
//SDL_Surface *screen;
public:
static SDL_Surface *screen;
static void set_screen(SDL_Surface *_screen);
static void set_pixel(int x, int y, Uint8 color);
static void clear_screen(int r, int g, int b);
static SDL_Surface* load_image(char path[500]);
static void draw_image(SDL_Surface *img, int x, int y, int width, int height);
static void draw_line(int x1, int y1, int x2, int y2, Uint8 color);
};
#endif
I am calling Wrapper::set_screen(screen) from another file and I get this error:
In file included from /home/david/src/aships/src/Wrapper.cpp:6:0:
/home/david/src/aships/src/Wrapper.hpp: In static member function ‘static void Wrapper::set_screen(SDL_Surface*)’:
/home/david/src/aships/src/Wrapper.hpp:11:18: error: invalid use of member ‘Wrapper::screen’ in static member function
/home/david/src/aships/src/Wrapper.cpp:10:3: error: from this location
I also get a similar error for the definition of every single function on Wrapper.cpp, for example:
void Wrapper::set_pixel(int x, int y, Uint8 color)
{
/* Draws a pixel on the screen at (x, y) with color 'color' */
Uint8 *p;
p = (Uint8 *) screen->pixels + y * screen->pitch + x * screen->format->BytesPerPixel;
*p = color;
}
On compile:
/home/david/src/aships/src/Wrapper.hpp: In static member function ‘static void Wrapper::set_pixel(int, int, Uint8)’:
/home/david/src/aships/src/Wrapper.hpp:11:18: error: invalid use of member ‘Wrapper::screen’ in static member function
/home/david/src/aships/src/Wrapper.cpp:17:17: error: from this location
I know it's related to the class being static and thus the variable Wrapper.screen is not accessible or something, but I'm not sure of how to fix it. Any ideas?
Your class and member (screen) are not static, which means they don't actually exist. You can't access a non static member in a static function.
Try to make your data members to be static.
I'm not convinced that the code abstract you show us is an accurate characterization of your problem.
Your header should not include
using namespace std;
— it doesn't use or declare anything from thestd
namespace, and specifyingusing namespace std;
is generally regarded as 'not a good idea', doubly so when it appears in a header file.It also isn't clear that your header needs to include
SDL/SDL.h
. If theUint8
type is easily isolated (not necessarily valid), then your header file can simply use a forward declaration of theSDL_Surface
class. (Your implementation code will need to includeSDL/SDL.h
; but you should not burden the users of your wrapper class with unnecessary#include
directives when simple forward declarations would suffice.)This code is self-contained (does not need any headers), but more or less simulates what you could use, and it compiles OK:
It also compiles without warnings. The
(Uint8 *)
cast (copied from the original) is unnecessary. With the class definition given, it is superfluous; if you are needing to use a cast because the type of thepixels
member ofSDL_Surface
actually isn'tUint8
, are you sure it is a good idea? And can't you usereinterpret_cast<Uint8>(screen->pixels)
instead to make it clearer?Can you reduce your problem to code analogous to this that still shows the actual error?
You are using a static variable
in your code.
In C++ when you declare a static variable in the .h (or .hpp) you are creating a variable that is general (static) to the class. Thus, to use it in another file you have to redeclare it (which I'm guessing you didn't) to create a variable in that file referencing the static one. In your case put this:
in the .cpp file.
I'm not sure the theory is well explained, but it works like that.