2013-06-30 139 views
3

成員的使用無效我有兩個班,這是其中的一個頭:錯誤:在靜態成員函數

#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不可訪問或東西,但我不知道如何解決它。有任何想法嗎?

+0

它應該工作...你能發佈一個最小的完整例子嗎? – Beta

+1

看來您正在嘗試訪問靜態函數內部的非靜態成員屏幕。你不能這麼做,因爲靜態成員函數並沒有用這個指針隱式地傳遞。因此,除非以某種方式使靜態函數對類實例可用,否則,您不能像在非靜態成員函數中那樣訪問非靜態成員。 –

+0

只要注意到你把成員改成了一個指針。確保你重新編譯了源代碼。 –

回答

0

你的類和成員(屏)不是靜態的,這意味着它們實際上並不存在。 您無法在靜態函數中訪問非靜態成員。

試着讓你的數據成員是靜態的。

3

您正在使用一個靜態變量

static SDL_Surface *screen; 
在你的代碼

在C++中,當你在.H(或.HPP)聲明靜態變量要創建一個變量,它是一般(靜態)的類。因此,要在另一個文件中使用它,必須重新聲明它(我猜你沒有這樣做),以便在引用靜態文件的文件中創建一個變量。在.cpp文件

SDL_Surface* Wrapper::screen; 

:你的情況,把這個。

我不知道該理論很好的解釋,但它的工作原理類似。

0

我不相信你告訴我們的代碼抽象是你的問題的準確表徵。

你的頭不應該包括using namespace std; - 當它出現在一個頭文件中不使用或宣佈從std命名空間中任何東西,並指定using namespace std;被普遍認爲是「不是一個好主意」,更是如此。

也不太清楚,你的頭需要包括SDL/SDL.h。如果Uint8類型很容易被隔離(不一定有效),那麼頭文件可以簡單地使用SDL_Surface類的前向聲明。 (您的實現代碼將需要包括SDL/SDL.h;但你不應該負擔的包裝類的不必要#include指令的用戶在簡單的前置聲明就足夠了)

這段代碼是獨立的(不需要任何頭) ,但更多或更少的模擬,你可以使用的東西,它編譯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; 
} 

它還編譯沒有警告。這個(Uint8 *)轉換(從原始複製)是不必要的。根據給出的類定義,這是多餘的;如果您需要使用演員陣容,因爲SDL_Surfacepixels成員的類型實際上不是Uint8,您確定這是個好主意嗎?你不能用reinterpret_cast<Uint8>(screen->pixels)來代替它嗎?


可以減少你的問題的代碼類似於這一點,仍然顯示實際的錯誤?