C/C++ C++ linking/inheritance problem?

  • Thread starter Thread starter FYAD
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion centers around a coding issue encountered while implementing a Steque class that inherits from both Stack and Queue classes in C++. The user faced a linker error indicating an "undefined reference" to the Queue constructor when attempting to compile the Steque class. The problem was identified as the absence of a definition for the Queue constructor, which is necessary since the Steque constructor implicitly calls the base class constructor. The solution involved adding a definition for the Queue constructor in the Steque.cc file, resolving the compilation error. This highlights the importance of providing implementations for declared constructors in C++ inheritance scenarios.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
17
Views
2K
Replies
22
Views
3K
Replies
5
Views
1K
Replies
6
Views
3K
Replies
10
Views
10K
Replies
2
Views
3K
Replies
4
Views
3K
Back
Top