Sign in to follow this  
Followers 0
JohnOne

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

9 posts in this topic

#1 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites



Everything would probably work ok in the cpp file, definition and implementation. I guess the compiler doesn't like implementations in header files, not exactly that they are in the *same* file.

1 person likes this

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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).

1 person likes this

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

You need to make the distinction between declaring a name and defining it. Declarations can occur repeatedly (forward declarations as well as extern (meaning another file)) without problems, but definitions should only occur once.

Share this post


Link to post
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
Sign in to follow this  
Followers 0