Error: invalid use of member in static member function

后端 未结 3 1021
野性不改
野性不改 2021-01-05 03:01

I have two classes, and this is the header of one of them:

#ifndef WRAPPER_HPP
#define WRAPPER_HPP

#include 

using namespace std;

class W         


        
相关标签:
3条回答
  • 2021-01-05 03:16

    You are using a static variable

    static SDL_Surface *screen;
    

    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:

    SDL_Surface* Wrapper::screen;
    

    in the .cpp file.

    I'm not sure the theory is well explained, but it works like that.

    0 讨论(0)
  • 2021-01-05 03:24

    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 the std namespace, and specifying using 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 the Uint8 type is easily isolated (not necessarily valid), then your header file can simply use a forward declaration of the SDL_Surface class. (Your implementation code will need to include SDL/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:

    #ifndef WRAPPER_HPP
    #define WRAPPER_HPP
    
    typedef unsigned char Uint8;
    class SDL_Surface;
    
    class Wrapper
    {
    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
    
    //#include <SDL/SDL.h>
    
    typedef unsigned short Uint16;
    
    class SDL_Surface
    {
    public:
        Uint8   *pixels;
        Uint16   pitch;
        struct
        {
            Uint8 BytesPerPixel;
        }       *format;
    };
    
    // End of SDL/SDL.h
    
    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;
    }
    

    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 the pixels member of SDL_Surface actually isn't Uint8, are you sure it is a good idea? And can't you use reinterpret_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?

    0 讨论(0)
  • 2021-01-05 03:34

    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.

    0 讨论(0)
提交回复
热议问题