unresolved external

Started by
4 comments, last by MaulingMonkey 15 years, 3 months ago
Hi all. I am trying to find the problem for error LNK2019 under visual studio 2005. Here is what I am doing I have a drawing function which draws a cube. I am using //Shape.h void DrawCube();//global function decl //Shape.cpp void DrawCube()//implementation { ... } //Header.h #include "Shape.h" #include "MyObj.h" //MyObj.cpp #include "Header.h //master include void MyObj::Render()//MyObj class tries to call global DrawCube() { DrawCube();//LNK2019; } moving DrawCube inside a class and making it static solves the problem, but I really wanna know why its giving me the linking error. Also I did used extern DrawCube(), at top of MyObj.cpp, but that didnt help either. Can you please figure out where I am getting wrong. Any help will be appreciated. Regards Kazz
Advertisement
LNK2019

Your code should work fine as shown -- please always post your real code (use copy and paste -- always!).

Common causes for this kind of error with code similar to what you've posted are:

1) Making a typo in the implementation/definition (example: DrawCUBE instead of DrawCube -- C++ is case sensitive)
2) Getting the parameters wrong (example: Declaring "void DrawCube();" but defining "int DrawCube() { ... }")
3) Getting the scope wrong (example: Declaring "namespace A { void DrawCube(); }" but defining "namespace B { void DrawCube() { ... } }"

For future reference, please also always paste the entire text of an error message. Most people don't have error codes memorized, and googling their MSDN pages will only get part of the information.
Nevermind. I don't want to confuse people with this post which contained incorrect info. Thank you MaulingMonkey for the kind correction.

[Edited by - MikeTacular on January 14, 2009 9:34:49 PM]
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Quote:Original post by MikeTacular
Most of the errors MaulingMonkey posted are compile time errors, not link time errors.

Not so -- definition also counts as a declaration, and functions can be overloaded. I have personally encountered linker errors from every single one of the mistakes I listed at one point or another. Also, extern is only necessary for variables -- it should be irrelevant for functions like here.

They would be compile errors if they were member functions, but DrawCube doesn't seem to be.
Quote:Original post by MaulingMonkey
Quote:Original post by MikeTacular
Most of the errors MaulingMonkey posted are compile time errors, not link time errors.

Not so -- definition also counts as a declaration, and functions can be overloaded. I have personally encountered linker errors from every single one of the mistakes I listed at one point or another. Also, extern is only necessary for variables -- it should be irrelevant for functions like here.

They would be compile errors if they were member functions, but DrawCube doesn't seem to be.
[embarrass]

Looking back at your post, I see I misread it. When I first read it I was thinking of calling the function (i.e. calling DrawCUBE or A::DrawCube). Now that I pay a little more attention as I read it, I clearly see you were talking about the declaration not being the same as the definition.

I wasn't aware that extern was unnecessary for functions. On this MSDN page, it says "Declarations of variables and functions at file scope are external by default." I'm assuming that's why extern is not required for functions, but it's still required for external variables. Is this because you can't(?) separate the declaration and definition of a variable?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
It's complicated, but, yeah, sounds right -- extern just lets you declare a global without defining it, whereas functions have their own seperate syntax.

This topic is closed to new replies.

Advertisement