-1

I've done some searching and have seen this error but in many cases it was caused by missing an include. The header file is underneath "header files" in vs2019 as well

The error is caused in main at Leaf<int> leaf(3);

Here is my main.cpp

#include "heap.h"
#include <iostream>

using namespace std;

int main()
{
    //Need to complete testing from main
    Heap<int> heap(2);
    Leaf<int> leaf(3);

    //heap.insert(3);
}

Here is the relevant class in heap.h

template <typename T> class Leaf : public Node<T> {
private:
    T key;
public:
    Leaf(T value) { key = value; }
    virtual Leaf<T>* toLeaf() { return this; }
    virtual Leaf<T>* insert(T key, Node<T>*& created) {
        Leaf<T>* newLeaf = new Leaf<T>(key);
        created = newLeaf;
        return newLeaf;
    }
};

Error:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2001 unresolved external symbol "public: virtual class Leaf<int> * __cdecl Node<int>::insert(int,class Node<int> * &)" (?insert@?$Node@H@@UEAAPEAV?$Leaf@H@@HAEAPEAV1@@Z)    2-3-4 Heap  C:\Users\catcal\source\repos\2-3-4 Heap\2-3-4 Heap\main.obj 1   
C. Catt
  • 15
  • 6
  • Suggestion: the error message provided looks like it came from Visual Studio's Error List. You may find the full build output given in the Output Tab located not far from the Error List more useful. The Error list needs to look pretty so it condenses the information and often leaves stuff out. Sometimes the stuff it leaves out is exactly what you needed to know to crack the case. And even if it isn't, it's plaintext and cuts and pastes into a Stack Overflow question much more easily. – user4581301 Feb 14 '20 at 22:56
  • 3
    Where is the definition of the `Node` class? The linker, not the compiler, is complaining that it can't find the implementation of the `Node::insert()` method, which is separate from your `Leaf::insert()` method. – Remy Lebeau Feb 14 '20 at 23:02
  • Getting back on topic, it looks like the linker can't find the implementation of `Node`'s `insert` method. There are many many possible reasons for this from, "Oooops. Forgot." to [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) and unfortunately it's impossible to figure out which case best fits your code without a stupendous leap of logic that would probably make an answer inscrutable to future askers. – user4581301 Feb 14 '20 at 23:02

1 Answers1

0

Why does my heap.h give me a link error?

You can check to see what kind of link error this is by the defined error number and in this case, it is LNK2001. This is stating that you have an unresolved external symbol.

When you try to call a function within a translation unit - a *.cpp file that has not yet been defined will cause the Linker to fail. It will generate the LNK2001 error that you are seeing. It simply doesn't know what to do with the function that you are trying to call simply because it has not yet been defined.

Looking closer at the description tells you what is causing this error flag to be triggered by the linker. It states that the Node<int>::insert(int,class Node<int> * &) can not be found or that there is no definition for it that is declared by public: virtual class Leaf<int> *

The reason you are seeing this is that in your main.cpp file you are making a call to both of Heap's and Leaf's constructors. And from your heap.h file you are also defining Leaf to be publicly inherited from Node. So when it tries to construct Leaf<T> it fails to do so because it can not completely construct Node<T>.

From the code that you have shown; you have declared and defined Leaf<T>::insert(T key, Node<T>*& created) but without seeing the reset of this header file for other possible includes as well as your the declaration and definition of your Node<T> class template, there is no easy way of knowing. However, I suspect that Node<T>::insert(T key, Node<int>* &) is not being defined or generated through template deduction since Node<T>::insert(T key, Node<T>* &) has not yet been defined.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59