1

I have problem while compiling two classes.

MemoryTrie.h:

#ifndef MEMORYTRIE_H
#define MEMORYTRIE_H

template <typename T>
class MemoryTrie
{
    public:
        ...
        T & operator [](const char * key);

    private:
        ...
};

#endif  /* MEMORYTRIE_H */

MemoryTrie.cpp:

#include "MemoryTrie.h"

template <typename T>
T & MemoryTrie<T>::operator [](const char * key)
{
    ...
}

OutputFileTrie.h:

#ifndef OUTPUTFILETRIE_H
#define OUTPUTFILETRIE_H

#include "MemoryTrie.h"

template <typename T>
class OutputFileTrie
{
    public:
        T & operator [](const char * key);

    private:
        MemoryTrie<T> memoryTrie;
};

#endif  /* OUTPUTFILETRIE_H */

OutputFileTrie.cpp:

#include "OutputFileTrie.h"

template <typename T>
T & OutputFileTrie<T>::operator [](const char * key)
{
    return memoryTrie[key];
}

And in main just:

OutputFileTrie<int> trie;

Linker says: OutputFileTrie.h:9: undefined reference to `MemoryTrie::MemoryTrie()'. I ran just these four commands:

g++ -Wall -pedantic -g -c MemoryTrie.cpp
g++ -Wall -pedantic -g -c OutputFileTrie.cpp
g++ -Wall -pedantic -g -c main.cpp
g++ MemoryTrie.o OutputFileTrie.o main.o -o out

Am I doing it right? Is there any mistake I missed? Thanks for any kind of help.

  • 1
    The template implementations must be in the header files. [This is discussed in this question, for example.](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Corey May 10 '13 at 17:47
  • 1
    Templates and inlines don't go in CPP files :) – RandyGaul May 10 '13 at 17:48

0 Answers0