Jump to content

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


Go to solution Solved by Ascend4nt,

Recommended Posts

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.

Posted

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.

Posted

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.

  • Solution
Posted

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

My contributions:

  Reveal hidden contents

Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFsProcess CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen)Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery

Wrappers/Modifications of others' contributions:

_DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity)

UDF's added support/programming to:

_ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne)

(All personal code/wrappers centrally located at Ascend4nt's AutoIT Code)

Posted

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.

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
  • Recently Browsing   0 members

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