错误:在静态成员函数中无效使用成员 [英] Error: invalid use of member in static member function

查看:3773
本文介绍了错误:在静态成员函数中无效使用成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,这是其中一个的标题:

  #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另一个文件,我得到这个错误:

 在/home/david/src/aships/src/Wrapper.cpp :6:0:
/home/david/src/aships/src/Wrapper.hpp:在静态成员函数'static void Wrapper :: set_screen(SDL_Surface *)':
/ home / david / src / aships / src / Wrapper.hpp:11:18:错误:在静态成员函数中使用成员Wrapper :: screen无效
/home/david/src/aships/src/Wrapper.cpp:10 :3:错误:从这个位置

我也得到类似的错误, on Wrapper.cpp,例如:

  void Wrapper :: set_pixel(int x,int y,Uint8 color)
{
/ *在颜色'color'的(x,y)的屏幕上绘制一个像素* /
Uint8 * p;
p =(Uint8 *)screen-> pixels + y * screen-> pitch + x * screen-> format-> BytesPerPixel;
* p = color;
}

编译时:

  /home/david/src/aships/src/Wrapper.hpp:在静态成员函数'static void Wrapper :: set_pixel(int,int,Uint8)':
/home/david/src/aships/src/Wrapper.hpp:11:18:错误:在静态成员函数中使用成员Wrapper :: screen无效
/ home / david / src / aships / src / Wrapper.cpp:17:17:错误:从这个位置

我知道它与类静态,因此变量Wrapper.screen是不可访问的或什么,但我不知道如何解决它。任何想法?

解决方案

您正在使用静态变量

  static SDL_Surface * screen; 




在您的代码中。



当你在.h(或.hpp)中声明一个静态变量时,你正在创建一个通用(静态)类的变量。因此,要在另一个文件中使用它,你必须重新声明它(我猜你没有)在该文件中创建一个引用静态的变量。在你的情况下,这样:

  SDL_Surface * Wrapper :: screen;  .cpp文件 

中的


我不确定这个理论是否有很好的解释,但它的工作原理。


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?

解决方案

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.

这篇关于错误:在静态成员函数中无效使用成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆