9

私には2つのクラスがあり、これはそのうちの1つのヘッダーです:

#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

別のファイルから Wrapper::set_screen(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 にアクセスできないか何かに関連していることはわかっていますが、修正方法がわかりません。何か案は?

4

3 に答える 3

1

クラスとメンバー (画面) は静的ではありません。つまり、実際には存在しません。静的関数で非静的メンバーにアクセスすることはできません。

データ メンバーを静的にするようにしてください。

于 2013-06-30T15:29:01.860 に答える
0

あなたが私たちに示したコードの要約があなたの問題の正確な特徴付けであるとは確信していません.

ヘッダーにはインクルードしないでください—名前空間using namespace std;から何も使用または宣言しません。また、指定することは一般に「お勧めできません」と見なされます。ヘッダー ファイルに表示される場合は二重にそうです。stdusing namespace std;

また、ヘッダーに を含める必要があることも明らかではありませんSDL/SDL.hUint8型が簡単に分離できる (必ずしも有効であるとは限らない) 場合、ヘッダー ファイルは単純にクラスの前方宣言を使用できますSDL_Surface。(実装コードには ; を含める必要がありますが、単純な前方宣言で十分な場合SDL/SDL.hは、ラッパー クラスのユーザーに不要なディレクティブを負担させないでください。)#include

このコードは自己完結型 (ヘッダーは必要ありません) ですが、多かれ少なかれ使用できるものをシミュレートしており、正常にコンパイルされます。

#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 *)コピー)は不要です。クラス定義が与えられているので、それは不要です。pixelsのメンバーの型がSDL_Surface実際には ではないためにキャストを使用する必要がある場合、Uint8それは良い考えですか? reinterpret_cast<Uint8>(screen->pixels)より明確にするために代わりに使用できませんか?


実際のエラーを表示するこれに類似したコードに問題を減らすことができますか?

于 2013-06-30T16:38:59.573 に答える