External Linking Errors!

How do you solve linking errors?

I have (2) errors, and I do not know how to fix them or even what to fix. I tried looking it up on the MSDN, but did not understand. I would appreciate any help I can get. Thanks!

The errors are displayed below:

Linking…
CanvasSnow.obj : error LNK2001: unresolved external symbol “public: __thiscall Canvas::Canvas(int,int,char *)” (??0Canvas@@QAE@HHPAD@Z)
Debug/CanvasSnow.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

CanvasSnow.exe - 2 error(s), 0 warning(s)

You don’t have the constructor defined anywhere in your project, only declared. Probably because:
[ul][]you didn’t write the function body.[]you didn’t include the correct source file.[*]you didn’t include the correct libray.[/ul]

Boy, that was a fast reply.

I thought this was my constructor (see below)?

Canvas(int width, int height, char* windowTitle); //constructor

I also have the below code near the top of my .CPP file, declared after my function prototypes.

Canvas cvs(640,480, “try out”); //global object

My program includes one header file which declares my class “Canvas” and includes (2) other classes as well. My first function definition for class Canvas is also included in the header file. It is as follows:

void Canvas::initCT(void)
{

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();  //set CT to the  
                       //identity matrix

}

I also have a “rotate2D” and a “scale2D” included in that same header file. Is this allowed, or do my function definitions need to be in a .cpp file (separate from the class Canvas.h file).

Thanks again!

Originally posted by kcay:
[b]I thought this was my constructor (see below)?

Canvas(int width, int height, char* windowTitle); //constructor
[/b]

That’s just the declaration, you actually have to have it do something. For example;

Canvas::Canvas( int width, int height, char* windowTitle )
{
m_nWidth = width;
m_nHeight = height;
m_pWindowTitle = windowTitle;
}

You told the compiler that there should be this constructor, but you never coded a constructor for it to do anything with.

I thought this was my constructor (see below)?

Canvas(int width, int height, char* windowTitle); //constructor

Yes, this is the constructor declaration, but you need the definition too; the code for it.

I also have the below code near the top of my .CPP file, declared after my function prototypes.

Canvas cvs(640,480, “try out”); //global object

This is probably where you want to create an object of type Canvas, but the linker can’t find it’s constructor, which the compiler want’s to call.

My program includes one header file which declares my class “Canvas” and includes (2) other classes as well. My first function definition for class Canvas is also included in the header file. […] Is this allowed, or do my function definitions need to be in a .cpp file (separate from the class Canvas.h file).

You can define functions in a header, but you should only do that if you know the side effects. If you don’t know that, you should separate interface (header) and implementation (source).

Thanks Bob–You’re right, I didn’t include anything like what you said in my program. I’m not quite sure what you meant by your code, “m_n”. What is “m_n” Here is what I added though as my function definition:

Canvas::Canvas(int width, int height, char windowTitle)
{
char
argv[1]; //dummy argument for glutinit
char dummyString[8];
argv[0] = dummyString; //hook up pointer
int argc = 1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(20,20);

setWindow(0,(float)width, 0, (float) height); //default wrld wndw
setViewport0,width,0,height);
CP.set(0.0f,0.0f);
CD = 0.0f; //the current direction in degrees

}

And here is my main:

int main( void)
{

cvs.initCT();

cvs.setViewport(0, 400, 0, 400);
cvs.setBackgroundColor(0.0,1.0,0.0);
cvs.setColor(0.0,0.0,1.0);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutDisplayFunc(myDisplay);
glutMainLoop();

return 0;
}

Although the program now compiles, I can’t execute because now I get a “send” or “don’t send” error report.

Do you know what the problem might be?

Thanks again!

Originally posted by kcay:
Thanks Bob–You’re right, I didn’t include anything like what you said in my program. I’m not quite sure what you meant by your code, “m_n”. What is “m_n” Here is what I added though as my function definition:

Sorry, the “m_n” is just a notation that is used by some people that lets you know it is a member variable(the m part) and then n lets you know it is a number.