views:

109

answers:

4

When I try to build my application the linker gives loads of errors like this one:

modlauch.obj : error LNK2005: "public: virtual __thiscall lolbutton::~lolbutton(void)" (??1lolbutton@@UAE@XZ) already defined in lolbutton.obj

I suspect it has something to do with misconfigured compiler but I don't know how to fix it. My class is only included once so I don't think it has anything to do with the code. I have tried rebuilding and cleaning the project but it didn't help.

Can someone suggest a solution to this problem? My platform is Win32(C++) and I'm using MFC.

+1  A: 

At a guess - without seeing the code - did you by any chance put the destructor for lolbutton in the header without declaring it inline? From your description this is the likely culprit if you end up with instances of the destructor in multiple translation units.

Timo Geusch
this shouldn't cause an error?
Elemental
It won't cause an error during compilation, but it will during linking.
Timo Geusch
+2  A: 

do you by any chance include your lolbutton.h file more than once? like so:

//file: something.h
#include <lolbutton.h>
//... do code

//file: something_other.h
#include <lolbutton.h>

//file: main.cpp
#include <something.h>
#include <something_other.h>
Ricardo Ferreira
This would cause a compile time error (symbol already defined) but seems the questioner is reporting a linker error
Elemental
A: 

I would go with either multiple includes of lolbutton.h, hence my comment about wrapping the contents in an "if !defined someUUID" block or perhaps it could be to do with the use of precompiled headers.

ChrisBD
I too think the issue is related to percompiled headers.
Andy
+2  A: 

You'll get the linker error when you wrote the class like this:

lolbutton.h:

class lolbutton {
public:
  virtual ~lolbutton();
};

lolbutton::~lolbutton() {
  // something...
}

You won't get it when you write it like this:

class lolbutton {
public:
  virtual ~lolbutton()
  { 
     // inlined something...
  }
};

Fix the linker error by moving the destructor definition from the .h file to a .cpp file. This ensures there is only one definition of the destructor.

Hans Passant