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

-1

I want to apply selection operation in a 3d viewer (using qt-creator)

any.h file:

#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 file :

#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();
}

main file:

#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();
}

the error :

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

when I try to make board static it shows a lot of errors about :

 26: undefined reference to `any::board'
c++
qt
compiler-errors
computational-geometry
asked on Stack Overflow Jul 14, 2019 by user3019180 • edited Jul 14, 2019 by Mureinik

2 Answers

1

board is an instance member. You can't use it in a static method like drawSquares. You either need to make board a static member too, or, more likely, make drawSquares non-static.

answered on Stack Overflow Jul 14, 2019 by Mureinik
0

Static function cannot access non-static variables

In your source code, compiler will flash an error i.e. illegal reference to non-static member any::board.

Hence, accessing non-static members from static method in C++ programming is not possible.

static method memories will be create once without creating an instance of class. so you cannot use of a non-static class members in a static method, because non-static members has no memory already in static methods.

Please define your methods as non-static or use of static members in your class.

Please check out this link.

answered on Stack Overflow Jul 14, 2019 by BattleTested

User contributions licensed under CC BY-SA 3.0