Jump to content
  • Advertisement
Sign in to follow this  
sfx81

unresolved external

This topic is 3418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
It's complicated, but, yeah, sounds right -- extern just lets you declare a global without defining it, whereas functions have their own seperate syntax.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!