【问题标题】:template class does not recnognize template argument模板类不识别模板参数
【发布时间】:2015-04-13 02:54:38
【问题描述】:

我正在尝试编译以下代码。但是,由于某种原因,编译失败。错误很多,但都与模板中的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)

【问题讨论】:

  • 当你解决编译问题时,阅读stackoverflow.com/questions/495021/…
  • 模板只能在头文件中定义。 stackoverflow.com/questions/495021/…
  • 好的。但我已经在 CPP 文件中进行了明确的实例化。那怎么了?
  • 嗨,我发现了问题。 :/ 我没有将模板关键字放在某些方法中,例如:ContainerType &amp; SteinerTreeObserver&lt;ContainerType&gt;::get_container () {return m_ec;} 谢谢!!

标签: c++ templates compilation g++4.8


【解决方案1】:

好的。但我已经在 CPP 文件中进行了明确的实例化。那么怎么了? – 罗梅里托 3 小时前 您在哪里进行了显式实例化?

您必须在标头中包含您的 cpp (#include "steiner_tree_observer.cpp") 并将它们从构建中排除,并从标头中删除“#include "steiner_tree_observer.h""。这是因为模板函数定义在实例化时总是必须知道,而不是像非模板函数/类。

顺便说一句。如果您导出该类,则模板类的显式实例化才有意义。所以您可以确定,您的链接器将从您编译的 DLL 或 Lib 中取出符号。

【讨论】:

  • 在 CPP 文件中。 template class SteinerTreeObserver&lt;EdgeContainer&lt;Comparator, HCell&gt;;
  • 当头文件被包含时,您的编译器需要实现。只有导出类时,这样的前向声明才有意义。
猜你喜欢
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
相关资源
最近更新 更多