thexpressive thexpressive - 2 months ago 24
C++ Question

gcc/c++ - undefined reference / class error

I forced to learn C++ and OpenGL development and following a few tutorial based on MFC and I tried to convert it to linux with gcc.

Most of them succeed, but this one is giving me this error when compiling.

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


This is how i compile it.

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


14new.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 world\n");
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;
}

Answer

Problem lies here: Titik2D shearing(Titik2D p, int h) {}

You defined a free function, NOT a member function.

Solution:

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