3

I'm trying to import some working Visual C++ code into Qt Creator and I'm having some compilation error I can't explain. This is my code:

// TabSprite.h

#include <GraphicsItem.h>

namespace appetizer {

class TabSprite : public GraphicsItem {

public:

  TabSprite(GraphicsWindow* parentWindow);

}

// TabSprite.cpp

#include <TabSprite.h>
using namespace appetizer;

TabSprite::TabSprite(GraphicsWindow* parentWindow): GraphicsItem(parentWindow) {

}

And Qt Creator give me this error (at the constructor declaration in TabSprite.cpp):

Undefined reference to 'appetizer::GraphicsItem::GraphicsItem(appetizer::GraphicsWindow*)

However GraphicsItem is properly declared in GraphicsItem.h so I don't understand why the compilers doesn't find it. Can anybody see what could be wrong with this code?

rubenvb
  • 74,642
  • 33
  • 187
  • 332
laurent
  • 88,262
  • 77
  • 290
  • 428
  • 1
    BTW: afaik there's a difference between defining and declaring: In TabSprite.h you declare the ctor of TabSprite and in TabSprite.cpp you define it. – Karl von Moor Jan 03 '11 at 14:05
  • I clarified my code - i should indeed have written "declared" and not "defined". I still can't get the code to compile though. – laurent Jan 03 '11 at 14:09
  • 1
    And I think it's good practise to use `namespace appetizer { /* definition of TabSprite*/ }` rather than `using namespace` which is sometimes critical. And: It's not a compilation but a linker error – Karl von Moor Jan 03 '11 at 14:09
  • @Polybos, it could also be defined in TabSprite.h if it's inline. Should be fine either way, as long as it is defined somewhere. – Sergei Tachenov Jan 03 '11 at 14:10
  • If you want your name to appear please set it in your SO preferences. – ismail Jan 03 '11 at 14:11
  • @Sergey Tachenov: Of course, but I wanted to say where declaration in definition is in his case :) – Karl von Moor Jan 03 '11 at 14:11
  • @user561309, the error you gave is a linking error. So you can get it to compile, you can't get it to link. – Sergei Tachenov Jan 03 '11 at 14:11

2 Answers2

3

That means that you didn't define the constructor of GraphicsItem.

Karl von Moor
  • 8,484
  • 4
  • 40
  • 52
  • 1
    Sorry I didn't include the whole code: GraphicsItem is defined in GraphicsItem.h, which itself is included in TabSprite.h. I've updated the code to show that. – laurent Jan 03 '11 at 14:07
  • 1
    Oh well you were right actually. The GraphicsItem.h file was there but not included in the .pro file so it wasn't being compiled. Problem solved :) – laurent Jan 03 '11 at 14:11
0

The constructor is probably declared in TabSprite.h (although your code doesn't show that). That's why it compiles. But the constructor isn't defined anywhere, or, less likely, the unit where it is defined isn't included in the linking process. That's why it doesn't link.

Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73