Thread: Linker Error: already defined?

  1. #1
    Registered User
    Join Date
    Apr 2005

    Linker Error: already defined?

    Hi, I'm creating my own text editor to put into practice what I know about the Win32 API. I'm working on a class to manage the Rich Edit control, and I've just made a class and a function for debug puposes to notify me when errors come up with a bit of information about the error.

    I'm using Visual C++ .NET 2003 Standard and when I try to compile & link my project, I get the following error:
    Notepad error LNK2005: "void __cdecl ShowError(struct SError &)" (?ShowError@@YAXAAUSError@@@Z) already defined in main.obj
    I know what this error means, but unfortunately I'm very confused as to what is causing this error... I'm sure I'm including my headers in the correct order... Here is the order in which my functions are being included in my project:

            CSearch.h       CFileManager.h
           /         \     /              \
          /           \   /                \
         /             \ /                  \
    CSeach.cpp    CTextManager.h	 CFileManager.cpp
    	     /		  \
                /              \
               /                \
           main.cpp         CTextManager.cpp
    And here is Error.h (where ShowError() is defined)

    #ifndef ERROR_H
    #define ERROR_H
    #include <string>
    struct SError
    	SError(char *szFunction, char *szError, char *szDescription)
    		m_szFunction = szFunction;
    		m_szError = szError;
    		m_szDescription = szDescription;
    	std::string m_szFunction;		// Function where error occured
    	std::string m_szError;			// Error title
    	std::string m_szDescription;	// Error description
    void ShowError(SError &error)
    	// Format error string
    	std::string szBuffer;
    	szBuffer = "Error in Function: \"";
    	szBuffer += error.m_szFunction;
    	szBuffer += "\"\n";
    	szBuffer += "Error: ";
    	szBuffer += error.m_szError;
    	szBuffer += "\nDetail: ";
    	szBuffer += error.m_szDescription;
    	// Show error
    	MessageBox(0,, "Error!", MB_ICONERROR);
    Perhaps I am just overlooking something, or am just doing something wrong. But I'd be grateful if someone could help resolve this error for me, I dont get this error very often as I haven't really taken on any projects of this size...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Each compilation unit (basically each cpp file) that includes Error.h gets a copy of that function definition. When the linker links the object files from the compilation of each cpp files, it gets multiple versions of that function. What you need to do is implement the function in a single cpp file, and just leave the prototype in Error.h. Alternatively, you might be able to mark the function as inline to resolve the problem.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Ah, that clears things up a bit. I guess being lazy and shoving implementations in headers messes things up a bit :P

    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Variables already defined while linking.
    By xconspirisist in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2005, 05:20 AM
  4. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 04:30 AM