成員的使用無效我有兩個班,這是其中的一個頭:錯誤:在靜態成員函數
#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
我從另一個文件,我調用包裝:: set_screen(屏)得到這個錯誤:
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
我也得到有關Wrapper.cpp每一個函數的定義類似的錯誤,例如:
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;
}
在編譯:
/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
我知道它的相關的類是靜態的,因此變量Wrapper.screen不可訪問或東西,但我不知道如何解決它。有任何想法嗎?
它應該工作...你能發佈一個最小的完整例子嗎? – Beta
看來您正在嘗試訪問靜態函數內部的非靜態成員屏幕。你不能這麼做,因爲靜態成員函數並沒有用這個指針隱式地傳遞。因此,除非以某種方式使靜態函數對類實例可用,否則,您不能像在非靜態成員函數中那樣訪問非靜態成員。 –
只要注意到你把成員改成了一個指針。確保你重新編譯了源代碼。 –