gpt4 book ai didi

c++ - 在静态成员函数中无效使用成员 ‘xx::x’

转载 作者:行者123 更新时间:2023-12-02 10:53:25 24 4
gpt4 key购买 nike

我想在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'

最佳答案

静态函数无法访问非静态变量

在您的源代码中,编译器将闪烁错误,即非法引用了非静态成员any::board

因此,无法通过C++编程中的静态方法访问非静态成员。
static方法的内存将只创建一次,而不创建class的实例。因此您不能在non-static方法中使用static类成员,因为non-static成员中已经没有static成员了。

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

请 checkout this link

关于c++ - 在静态成员函数中无效使用成员 ‘xx::x’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57025709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com