C++ linking/inheritance problem?

  • Thread starter FYAD
  • Start date
  • #1
5
0
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.

Queue.h

Code:
#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{

 public:
  Queue();
  ~Queue();
.
.
.
more stuff
.

Steque.h

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

class Steque : virtual public Queue{
 public:
  Steque();
};


Steque.cc

#include "Steque.h"


Steque::Steque(){
}

main(){

}
When I try to run it:

Code:
$ 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.
 

Answers and Replies

  • #2
5
0
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.
 
  • #3
plover
Homework Helper
188
0
As best I can tell, you're missing the definition for the Queue constructor. In other words, somewhere you need to add:
Code:
Queue::Queue() { }
 
  • #4
plover
Homework Helper
188
0
FYAD said:
I put a Queue constructor in my Steque.cc class and it worked. I'm not sure why, but it did.
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.
 

Related Threads on C++ linking/inheritance problem?

  • Last Post
Replies
5
Views
3K
Replies
8
Views
1K
  • Last Post
Replies
9
Views
3K
  • Last Post
Replies
3
Views
5K
  • Last Post
Replies
9
Views
2K
Replies
4
Views
629
  • Last Post
Replies
3
Views
17K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
2
Views
2K
Replies
1
Views
2K
Top