Я пытаюсь скомпилировать следующий код. Но по какой-то причине компиляция терпит неудачу. Есть много ошибок, но все они связаны с TYPE в шаблоне.шаблон класс не распознает аргумент шаблона
Я изучил код, и это похоже. Я не вижу проблемы. Это может быть глупая ошибка, но не вижу. Может ли кто-нибудь помочь?
заголовок файла:
namespace rca {
template<class ContainerType>
class SteinerTreeObserver {
public:
SteinerTreeObserver();
SteinerTreeObserver(ContainerType & ec, STTree & st, int);
void set_steiner_tree (STTree &st, int);
STTree & get_steiner_tree();
void set_container (ContainerType & ec);
ContainerType & get_container();
bool add_edge (int, int, int, int);
void prunning (int, int);
private:
ContainerType m_ec;
STTree m_st;
DisjointSet2 dset;
};
};
Вот файл CPP:
#include "steiner_tree_observer.h"
using namespace rca;
template<class ContainerType>
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(){}
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes){}
template<class ContainerType>
void SteinerTreeObserver<ContainerType>::set_steiner_tree (STTree & st, int nodes){}
template<class ContainerType>
STTree & SteinerTreeObserver<ContainerType>::get_steiner_tree()
{ return m_st;}
void SteinerTreeObserver<ContainerType>::set_container (ContainerType & ec)
{}
ContainerType & SteinerTreeObserver<ContainerType>::get_container()
{return m_ec;}
template<class ContainerType>
bool SteinerTreeObserver<ContainerType>::add_edge (int x,
int y,
int cost,
int band_usage)
{ return true;}
template<class ContainerType>
void SteinerTreeObserver<ContainerType>::prunning (int rest, int band)
{}
template class SteinerTreeObserver<EdgeContainer<Comparator, HCell>>;
Выход:
steiner_tree_observer.cpp: At global scope:
steiner_tree_observer.cpp:10:21: error: ‘ContainerType’ was not declared in this scope
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:10:34: error: template argument 1 is invalid
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:10:56: error: invalid type in declaration before ‘(’ token
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:10:57: error: ‘ContainerType’ was not declared in this scope
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:10:73: error: ‘ec’ was not declared in this scope
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:10:83: error: expected primary-expression before ‘&’ token
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:10:85: error: ‘st’ was not declared in this scope
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:10:88: error: expected primary-expression before ‘int’
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:10:97: error: expression list treated as compound expression in initializer [-fpermissive]
SteinerTreeObserver<ContainerType>::SteinerTreeObserver(ContainerType & ec,STTree & st,int nodes)
^
steiner_tree_observer.cpp:11:1: error: expected ‘,’ or ‘;’ before ‘{’ token {
steiner_tree_observer.cpp:18:25: error: expected initializer before ‘<’ token
void SteinerTreeObserver<ContainerType>::set_steiner_tree (STTree & st, int nodes)
^
steiner_tree_observer.cpp:25:29: error: expected initializer before ‘<’ token
STTree & SteinerTreeObserver<ContainerType>::get_steiner_tree()
^
steiner_tree_observer.cpp:30:25: error: expected initializer before ‘<’ token
void SteinerTreeObserver<ContainerType>::set_container (ContainerType & ec)
^
steiner_tree_observer.cpp:35:1: error: ‘ContainerType’ does not name a type
ContainerType & SteinerTreeObserver<ContainerType>::get_container()
^
steiner_tree_observer.cpp:41:25: error: expected initializer before ‘<’ token
bool SteinerTreeObserver<ContainerType>::add_edge (int x,
^
steiner_tree_observer.cpp:64:25: error: expected initializer before ‘<’ token
void SteinerTreeObserver<ContainerType>::prunning (int rest, int band)
Когда вы закончите компиляцию, прочитайте http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – chris
Шаблоны могут быть определены только в заголовочный файл. http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file?lq=1 – ZachSand
ОК. Но я сделал явное создание экземпляра в файле CPP. Так что не так? – Romerito