Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ linking/inheritance problem?

  1. Mar 2, 2005 #1
    I'm writing a Steque class that will inherit from my Stack and Queue classes. When I try to link it, I'm getting this weird error, which I'm sure has something to do with trying to inheritance.


    Code (Text):

    #include <iostream>
    #include <cstddef>
    #include <stdlib.h>

    using namespace std;
    typedef string Itemtype;
    struct NodeType;
    typedef NodeType *NodePtr;
    typedef struct NodeType{
      Itemtype item;
      NodePtr next;
      NodePtr last;

    class Queue{

    more stuff


    #include "Queue.h"
    //#include "Stack.h"

    class Steque : virtual public Queue{


    #include "Steque.h"




    When I try to run it:

    Code (Text):

    $ g++ -g -c Steque.cc
    $ make
    g++  -g -o Steque Steque.o
    Steque.o(.text+0x20): In function `Steque::Steque[in-charge]()':
    /home/john/Steque.cc:4: undefined reference to `Queue::Queue[not-in-charge]()'
    collect2: ld returned 1 exit status
    make: *** [Steque] Error 1
    In my steque.cc class, I have nothing but the contructor and main. I'm just trying to get it to compile before I start writing code for it. This is my third c++ program, so I'm not sure what the problem is.
  2. jcsd
  3. Mar 2, 2005 #2
    grr, two seconds after I posted this I figured it out. I put a Queue constructor in my Steque.cc class and it worked. I'm not sure why, but it did. Thanks for the potential replies.
  4. Mar 2, 2005 #3


    User Avatar
    Homework Helper

    As best I can tell, you're missing the definition for the Queue constructor. In other words, somewhere you need to add:
    Code (Text):
    Queue::Queue() { }
  5. Mar 2, 2005 #4


    User Avatar
    Homework Helper

    Your declaration of class Queue included a declaration of a constructor with no arguments. If that constructor is referenced anywhere, you must include a body for it.

    When the constructor to a derived class is called, this will call the constructor of the base class. Thus when the compiler processes Steque::Steque() it will include a reference to Queue::Queue(). Then when the linker tries to resolve that reference, it cannot find a body for Queue::Queue() and shows an error.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook