0

I have a generic class which in I also have a struct. Like this:

template <class T> class Octree
{
    public:

    struct node
    {
        typename  T value;
        node *child[8];
    };

    Octree();
    template <class T> Octree();
    ~Octree();
    void clear(T t);

    node* root;
};

the .cpp file :

template <class T>
Octree<T>::Octree()
{
    root = new node;
    root->value = 0;

    for (int i = 0; i < 8; i++)
        root->child[i] = 0;
}

template <class T>
Octree<T>::~Octree()
{
    clear(root);
}

and the main:

using namespace std;

int main() {
    Octree <int> fasz;
    Octree <char> fasz2;
    return 0;
}

I've just created an object of my tree and I got this error:

Error LNK2019 unresolved external symbol "public: __thiscall Octree::Octree(void)" (??0?$Octree@H@@QAE@XZ) referenced in function _main Zz c:\Users....Source.obj

I have tried to write the template declaration in a header file, then implement the class in an implementation file (for example .cpp), and include this implementation file at the end of the header but then I've got an error:

Error C2995 'Octree::Octree(void)': function template has already been defined

I have seen this link question but it's not what I need.

Have you any idea what the problem is?

Niall
  • 30,036
  • 10
  • 99
  • 142
T.dog
  • 91
  • 1
  • 12
  • That duplicate, and this answer as well; http://stackoverflow.com/a/12574417/3747990. You need to have the template implementations available at the time of instantiation (when it is used). – Niall May 17 '16 at 09:58
  • You also have a syntax issue; `template Octree();`, should just be `Octree();` in the class definition. – Niall May 17 '16 at 10:01
  • FYI, in C++ it's generally called _templates_, not generics. – user4520 May 17 '16 at 10:05
  • @Niall Thank you for the answer but when I copy that : `Octree::Octree() {}` on the bottom of the header then I got the same error: `Error C2995 'Octree::Octree(void)': function template has already been defined` – T.dog May 17 '16 at 10:07
  • Add some include guards (or a `#pragma once`) to avoid multiple inclusions. – Niall May 17 '16 at 10:08
  • @Niall Now I got a new error for the closing `}` of the constructor. `Error C2244 'Octree::Octree': unable to match function definition to an existing declaration ` Could you please help with this? – T.dog May 17 '16 at 10:23
  • Did you remove the `template Octree();` in the class definition? – Niall May 17 '16 at 10:36
  • Yes I did. Should I edit the quesiton? – T.dog May 17 '16 at 10:38

0 Answers0