在静态成员函数中无效使用成员“xx::x”

invalid use of member ‘xx::x’ in static member function

我想在 3d 查看器中应用选择操作(使用 qt-creator)

any.h 文件:

#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
class any
{
public:
    any();
     int board[3][3];
   void init(void);
  static void drawSquares(GLenum mode);
  static void processHits (GLint hits, GLuint buffer[]);
   static  void pickSquares(int button, int state, int x, int y);
   static void display(void);
  static void reshape(int w, int h);
};

any.ccp 文件:

#include "any.h"

any::any()
{

}

void any:: init(void)
{
   int i, j;
   for (i = 0; i < 3; i++)
      for (j = 0; j < 3; j ++)
         board[i][j] = 0;
   glClearColor (0.0, 0.0, 0.0, 0.0);
}

void any:: drawSquares(GLenum mode)
{
   GLuint i, j;
   for (i = 0; i < 3; i++) {
      if (mode == GL_SELECT)
         glLoadName (i);
      for (j = 0; j < 3; j ++) {
         if (mode == GL_SELECT)
            glPushName (j);
         glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0,(GLfloat) board[i][j]/3.0);
         glRecti (i, j, i+1, j+1);
         if (mode == GL_SELECT)
            glPopName ();
      }
   }
}

/*  processHits prints out the contents of the
 *  selection array.
 */
void any:: processHits (GLint hits, GLuint buffer[])
{
   unsigned int i, j;
   GLuint ii, jj, names, *ptr;

   printf ("hits = %d\n", hits);
   ptr = (GLuint *) buffer;
   for (i = 0; i < hits; i++) { /*  for each hit  */
      names = *ptr;
      printf (" number of names for this hit = %d\n", names);
         ptr++;
      printf("  z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
      printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
      printf ("   names are ");
      for (j = 0; j < names; j++) { /*  for each name */
         printf ("%d ", *ptr);
         if (j == 0)  /*  set row and column  */
            ii = *ptr;
         else if (j == 1)
            jj = *ptr;
         ptr++;
      }
      printf ("\n");
      board[ii][jj] = (board[ii][jj] + 1) % 3;
   }
}

#define BUFSIZE 512

void any:: pickSquares(int button, int state, int x, int y)
{
   GLuint selectBuf[BUFSIZE];
   GLint hits;
   GLint viewport[4];

   if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
      return;

   glGetIntegerv (GL_VIEWPORT, viewport);

   glSelectBuffer (BUFSIZE, selectBuf);
   (void) glRenderMode (GL_SELECT);

   glInitNames();
   glPushName(0);

   glMatrixMode (GL_PROJECTION);
   glPushMatrix ();
   glLoadIdentity ();
/*  create 5x5 pixel picking region near cursor location      */
   gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
                  5.0, 5.0, viewport);
   gluOrtho2D (0.0, 3.0, 0.0, 3.0);
   drawSquares (GL_SELECT);

   glMatrixMode (GL_PROJECTION);
   glPopMatrix ();
   glFlush ();

   hits = glRenderMode (GL_RENDER);
   processHits (hits, selectBuf);
   glutPostRedisplay();
}

void any:: display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   drawSquares (GL_RENDER);
   glFlush();
}

void any:: reshape(int w, int h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D (0.0, 3.0, 0.0, 3.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

主文件:

#include <any.h>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
 //  glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (100, 100);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   any* an=new any();
   an->init();

   glutMouseFunc (an->pickSquares);
   glutReshapeFunc (an->reshape);
   glutDisplayFunc(an->display);
   glutMainLoop();
   return app.exec();
}

错误:

../untitled/any.cpp:60:24: error: invalid use of member ‘any::board’ in static member function

当我尝试使板静态化时,它显示了很多关于 :

的错误
 26: undefined reference to `any::board'

board 是实例成员。你不能在像 drawSquares 这样的静态方法中使用它。您也需要使 board 成为静态成员,或者更有可能使 drawSquares non-static.

静态函数无法访问non-static变量

在您的源代码中,编译器会出现一个错误,即非法引用 non-static 成员 any::board

因此,在 C++ 编程中从静态方法访问 non-static 成员是不可能的。

static 方法记忆将被创建一次而不创建 class 的实例。所以你不能在 static 方法中使用 non-static class 成员,因为 non-static 成员在 static 方法中没有内存。

请将您的方法定义为 non-static 或在 class 中使用 static 成员。

请查看this link