-1

I have a number of objects, all of which have constructors that take no parameters. I defined the simple constructor of the base class, and I have not defined the others. When compiling, I receive an "Undeclared reference to constructor" error for each class, but I get an even more confusing error if I try to define the derived objects' constructors. The following is the "weird" error:

CMakeFiles/project4.dir/src/Strategy.cpp.o: In function `ForwardStrategy':
/home/ics45c/projects/p4/src/Strategy.cpp:42: undefined reference to `vtable for
ForwardStrategy'

My constructor for the base class looks like this:

ForwardStrategy::ForwardStrategy()
{
}

And all other constructors look like this (each with different numbers):

ForwardStrategy1::ForwardStrategy1()
{
}

Any help is greatly appreciated!

Edit: Here is the class declaration:

class ForwardStrategy
{
public:
    ForwardStrategy();
    virtual ~ForwardStrategy() = default;
    virtual bool isWorthForwarding(Message::Message* m) = 0;
    virtual void setType(unsigned int type);
    virtual void setQuality(unsigned int q);
private:
    unsigned int type;
    unsigned int quality;
};
MattDella
  • 15
  • 4

1 Answers1

0

Make sure your non-pure virtual methods are implemented somewhere and are being linked. See GCC FAQ:

Therefore, if you fail to define this particular method, the linker may complain about the lack of definitions for apparently unrelated symbols...

The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual.

Community
  • 1
  • 1
greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • Thank you for the response. I am still confused on _which_ methods to define. My base class declares 5 methods: a constructor, a destructor, two voids, and a bool. The constructor is declared as follows: ForwardStrategy() And the other methods: virtual ~ForwardStrategy() = default; virtual bool isWorthForwarding(Message::Message* m) = 0; virtual void setType(unsigned int type); virtual void setQuality(unsigned int q); I will post them above to make them more readable. – MattDella Nov 26 '13 at 01:54
  • Add it to your question please, stop trying to fit everything in a comment. – greatwolf Nov 26 '13 at 01:54
  • Done. My apologies, I'm new to this site, and I'm still figuring out how to correctly format things. – MattDella Nov 26 '13 at 01:58
  • Provide an implementation for `setType` and `setQuality` or make them pure virtual too `=0`. – greatwolf Nov 26 '13 at 02:23
  • I believe I have isolated the problem but can you attempt to quickly explain why this is? When I comment out the destructor of my base class, it compiles. Simply declaring it as virtual, or setting it equal to default causes it to fail. – MattDella Nov 26 '13 at 02:25
  • @MattDella it's a restriction placed on using `=default` on a virtual destructor. See [this question](http://stackoverflow.com/a/824185/234175). The new C++14 draft seems to allow it but in the meantime just give it an empty body `{}` instead. – greatwolf Nov 26 '13 at 02:33