C ++ inheritance problem

I am trying to implement a left tree using heaps as a base class. The following is the contents of heap.h:

template <class T>

class heap {
public:
    virtual void init(T*) = 0;
    virtual void insert(T*) = 0;
    virtual T delete_min() = 0;
};

The following is the contents of leftist.cpp:

#include "heap.h"

template <class T>
class leftist_tree : public heap<T> {
private:
    T* root;
public:
    void init(T* a) {}
    void insert(T* a) {}
    T delete_min() {T a; return a;}
};

I pass another leftist_node class as a parameter of this class using the following definition:

leftist_tree<leftist_node> mytree;

I get an LNK 2001 unresolved external character error for the init, insert, and delete_min functions. What am I doing wrong?

Edit:

Well, the example I gave at this moment is too complicated. I tried to reproduce the same error on a smaller scale so that someone could more easily identify the problem. I created the following sample files.

try.cpp

#include "stdafx.h"
#include "myclass.h"

int _tmain(int argc, _TCHAR* argv[])
{
    myclass<int> a;
    a.hello(3);
    return 0;
}

myclass.h

template <class T>

class myclass {
public:
    void hello(T);
};

myclass.cpp

#include "myclass.h"
#include <iostream>
using namespace std;

template <class T>
void myclass<T>::hello(T a){
    cout<<a<<endl;
    system("pause");
}

I get a similar error message:

1 > try.obj: LNK2001: "public: void __thiscall myclass:: hello (int)" (? hello @? $myclass @H @@QAEXH @Z) 1 > c:\users\meher anand\documents\visual studio 2010\Projects\try\Debug\try.exe: LNK1120: 1

, ?

+3
4

, . , . , , - .cpp, undefined, , - .

leftist.cpp, , - . .h, , , . - leftist.cpp, , - .

, .

+3

. - .cpp cpp, .

- .h.

+3

,

LNK20XX

, .

, . , .

+1
source

The following should work (tested with g ++):

// File: heap.hh --------------------------
template <class T>
class heap {
public:a
    virtual void init(T*) = 0;
    virtual void insert(T*) = 0;
    virtual T delete_min() = 0;
};

// File: leftist_tree.hh ------------------
#include "heap.hh"
template <class T>
class leftist_tree : public heap<T> {
private:
    T* root;
public:
    void init(T* ) {}
    void insert(T* ) {}
    T delete_min() {T a; return a;}
};

// File: leftist_node.hh ------------------
struct leftist_node {
    int value;
};

// File: leftist_main.cpp -----------------
#include "leftist_tree.hh"
#include "leftist_node.hh"

int main() {
    leftist_tree< leftist_node > mytree;
}
+1
source

Source: https://habr.com/ru/post/1767620/


All Articles