Jump to content

C++ error LNK2005: .... already defined


JohnOne
 Share

Go to solution Solved by Ascend4nt,

Recommended Posts

I have this error, and I cannot understand why it is happening.

I'm using VS 2010, win32 project.

The error says that the method Full is already defined in MyClass object.

Well yes, I know it is, it is a prototype of the implementation which is defined outside of the class.

If I comment out the implementation code the project compiles fine without issue.

Both class and method are in the same header file.

There are no other functions, methods, or vars with the same name of either class or method.

I've been trying to figure it out too long, and need some help if anyone can offer it.

class MyClass
{
    int cx, cy;
    HDC hdc, hDest;

public:
    MyClass() : cx(0), cy(0)
    {
        hdc = GetDC(NULL);
        hDest = CreateCompatibleDC(hdc);

    }

    MyClass(int vx, int vy)
    {
        hdc = GetDC(NULL);
        hDest = CreateCompatibleDC(hdc);
        cx = vx;
        cy = vy;
    }

    void Full(int, int, int, int, std::string);
    void Region(int, int, int, int, std::string);
};

void MyClass::Full(int x, int y, int w, int h, std::string path){

    return;
}
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

It has just occurred to me that this project is the first I have made where I specified no precompiled headers.

Setting to use precompiled headers fixes my problem.

Cheers for looking folks.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

This is cracking me up now.

As I said above setting properties of project to use precompiled headers worked (seemingly)

But after I added to the class in the same manner, the new method showed the same error, so I'm back to square one, being confused and frustrated.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Solution

John,

Because you declared that function outside of the class, for every C++ file that includes the header, duplicate versions of that function will be created - which is correct compiler behavior. That's why you should either declare the function in its own C++ file, declare it as inline, or put it inside the class object definition (which behaves like inline).

Link to comment
Share on other sites

Cheers, I've just capitulated and put the functions in their own cpp file.

For the record there is only one file that is included in all cpp files and that is stdafx.h, which has header guards and contains the other headers which the project is dependent on.

You know when you just know something is not quite right though?

Thanks for the explanation, glad to put this hiccup behind me.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...