C++ linking/inheritance problem?

  • C/C++
  • Thread starter FYAD
  • Start date
  • Tags
    C++
In summary, the issue with the Steque class inheriting from the Stack and Queue classes was due to a missing constructor definition for the Queue class. Once a constructor was added, the program compiled successfully.
  • #1
FYAD
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.
 
Technology news on Phys.org
  • #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.
 
  • #3
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
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.
 

1. What is the difference between static and dynamic linking in C++?

Static linking involves directly including code from external libraries into the final executable file, while dynamic linking involves loading the code from external libraries at runtime. Dynamic linking allows for smaller executable files and easier updates, while static linking can result in faster program execution.

2. How does inheritance work in C++?

Inheritance is a feature in C++ that allows a class to inherit properties and methods from another class. The class that inherits from another is called a derived class, while the class it inherits from is called the base class. The derived class can access all public and protected members of the base class, and can also override or add new functionality to those members.

3. What is the diamond problem in C++ inheritance?

The diamond problem occurs when a class inherits from two classes that have a common base class. This results in the derived class having multiple copies of the base class, which can lead to ambiguity and errors when trying to access inherited members. To avoid this problem, virtual inheritance can be used to ensure that there is only one instance of the base class.

4. How can I resolve linker errors in C++?

Linker errors occur when the compiler is unable to find the definitions of functions or classes that have been declared but not defined. This can happen if the appropriate libraries are not included or if the definitions are missing. To resolve linker errors, ensure that all necessary libraries are included and that all declared functions and classes have corresponding definitions.

5. Can I use multiple inheritance in C++?

Yes, C++ allows for multiple inheritance where a class can inherit from multiple base classes. However, this can also lead to the diamond problem and other complexities, so it should be used carefully. Alternatively, composition can be used to achieve similar functionality without the potential issues of multiple inheritance.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
10
Views
9K
Back
Top