Gcc /undefined引用/类错误



我被迫学习c++和OpenGL开发,并遵循一些基于MFC的教程,我试图用gcc将其转换为linux。

大多数都成功了,但是这个在编译时给了我这个错误。

/tmp/ccUmEDkj.o: In function `display()':
14new.cc:(.text+0x1ee): undefined reference to `Titik2D::shearing(Titik2D, int)'
collect2: error: ld returned 1 exit status

我是这样编译它的

gcc 14new.cc -lGL -lglut -lGLU -o 14
14 new.cc

#include <stdio.h>
#include <GL/freeglut.h>
GLsizei wh = 600 ; // initial height of window
GLsizei ww = 800 ; // initial width of window
class Titik2D {
    public: int x,y;
    Titik2D shearing(Titik2D p, int h);
};
Titik2D shearing(Titik2D p, int h)
{
    Titik2D temp;
    temp.x=p.x + h*p.y;
    temp.y=p.y;
    return temp;
}
void display(void)
{
    glClear ( GL_COLOR_BUFFER_BIT ); //clear pixel buffer
    Titik2D hit;
    int i;
    Titik2D p[11], q[11];
    p[0].x=10; p[0].y=10;
    p[1].x=50; p[1].y=10;
    p[2].x=50; p[2].y=100;
    p[3].x=100; p[3].y=100;
    p[4].x=100; p[4].y=150;
    p[5].x=50; p[5].y=150;
    p[6].x=50; p[6].y=250;
    p[7].x=150; p[7].y=250;
    p[8].x=150; p[8].y=300;
    p[9].x=10; p[9].y=300;
    p[10].x=10; p[10].y=10;
    glColor3f(1,0,0);
    glBegin(GL_LINE_STRIP);
        for (i=0; i<= 10; i++)
            glVertex2i(p[i].x, p[i].y);
    glEnd();
    for (i=0; i<= 10; i++){
        q[i]= hit.shearing(p[i],2);
    }
    glBegin(GL_LINE_STRIP);
    for (i=0; i <=10; i++)
    glVertex2i(q[i].x, q[i].y);
    glEnd();
    glFlush();
}
void MyInit ( void ) {
    glClearColor ( 1.0, 1.0, 1.0, 0.0 ); //white background
    glColor3f(1, 0, 0); // red drawing colour
    glMatrixMode ( GL_PROJECTION );
    glLoadIdentity ();
    gluOrtho2D ( 0.0, (GLdouble)ww, 0.0, (GLdouble)wh ); //Display area
}

int main(int argc, char **argv)
{
    printf("hello worldn");
    glutInit(&argc, argv);
    glutInitDisplayMode
    ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH );
    glutInitWindowSize(ww,wh);
    glutInitWindowPosition(180,90); //position on screen
    glutCreateWindow("opengl window");

    MyInit();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

问题在这里:Titik2D shearing(Titik2D p, int h) {}

你定义了一个自由函数,而不是成员函数。

解决方案:

Titik2D Titik2D::shearing(Titik2D p, int h) {}

您在剪切定义中忘记了作用域Titik2D::。您没有定义成员函数

Titik2D Titik2D::shearing(Titik2D p, int h)

你正在声明和定义一个自由函数

Titik2D shearing(Titik2D p, int h)

因此你得到一个链接错误。

而GCC是针对C语言的。在c++中使用g++(和cstdio)

相关内容

最新更新