C++ linking/inheritance problem?

  • Context: C/C++ 
  • Thread starter Thread starter FYAD
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around a C++ linking issue related to inheritance, specifically involving a Steque class that inherits from Queue and potentially Stack classes. Participants explore the error encountered during compilation and linking, focusing on constructor definitions and their implications in class inheritance.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes encountering an "undefined reference" error related to the Queue constructor when trying to compile the Steque class.
  • Another participant suggests that the missing definition for the Queue constructor is likely the cause of the error, indicating that a constructor body must be provided if it is declared.
  • A participant notes that adding a Queue constructor definition in the Steque.cc file resolved the issue, although they express uncertainty about why this was necessary.
  • Further clarification is provided about how the base class constructor is called when a derived class constructor is invoked, emphasizing the need for a defined body for the base class constructor to avoid linker errors.

Areas of Agreement / Disagreement

Participants generally agree that the absence of a constructor definition for Queue leads to the linking error, but there is some uncertainty regarding the underlying reasons for this requirement.

Contextual Notes

The discussion highlights the importance of providing definitions for declared constructors in C++ classes, particularly in the context of inheritance. However, specific assumptions about the broader implications of constructor behavior in C++ are not fully explored.

Who May Find This Useful

Readers interested in C++ programming, particularly those dealing with class inheritance and linking issues, may find this discussion relevant.

FYAD
Messages
5
Reaction score
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.
 
Technology news on Phys.org
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.
 
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() { }
 
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.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 10 ·
Replies
10
Views
11K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K