C++ 模板类不重新命名模板参数
我正在尝试编译以下代码。但是,由于某种原因,编译失败了。有很多错误,但都与模板中的类型有关 我已经检查了代码,它看起来像。我看不出有什么问题。 这可能是一个愚蠢的错误,但你不明白。有人能帮忙吗 头文件:C++ 模板类不重新命名模板参数,c++,templates,compilation,g++4.8,C++,Templates,Compilation,G++4.8,我正在尝试编译以下代码。但是,由于某种原因,编译失败了。有很多错误,但都与模板中的类型有关 我已经检查了代码,它看起来像。我看不出有什么问题。 这可能是一个愚蠢的错误,但你不明白。有人能帮忙吗 头文件: 名称空间rca{ 模板 类SteinerTreeObserver{ 公众: SteinerTreeObserver(); SteinerTreeObserver(ContainerType&ec、STTree&st、int); 空集_steiner_树(STTree&st,int); STTre
名称空间rca{
模板
类SteinerTreeObserver{
公众:
SteinerTreeObserver();
SteinerTreeObserver(ContainerType&ec、STTree&st、int);
空集_steiner_树(STTree&st,int);
STTree&get_steiner_tree();
空套容器(容器类型&ec);
ContainerType&get_container();
布尔加_边(int,int,int,int);
空洞修剪(int,int);
私人:
集装箱类型m_ec;
STTree m_街;
不相交的数据集;
};
};
以下是cpp文件:
#包括“steiner_tree_observer.h”
使用名称空间rca;
模板
SteinerTreeObserver::SteinerTreeObserver(){}
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec,STTree&st,int节点){}
模板
void SteinerTreeObserver::set_steiner_树(STTree&st,int节点){}
模板
STTree&SteinerTreeObserver::get_steiner_tree()
{返回m_st;}
void SteinerTreeObserver::set_容器(ContainerType&ec)
{}
ContainerType&SteinerTreeObserver::get_容器()
{返回m_ec;}
模板
boolsteinertreeobserver::add_edge(intx,
INTY,
整数成本,
int波段(U使用)
{返回true;}
模板
void SteinerTreeObserver::修剪(int-rest,int-band)
{}
模板类SteinerTreeObserver;
输出:
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)
steiner\u tree\u observer.cpp:在全局范围内:
steiner_tree_observer.cpp:10:21:错误:“ContainerType”未在此范围内声明
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:10:34:错误:模板参数1无效
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:10:56:错误:“(”标记之前的声明中的类型无效
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:10:57:错误:“ContainerType”未在此范围内声明
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:10:73:错误:“ec”未在此范围内声明
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:10:83:错误:应在“&”标记之前使用主表达式
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:10:85:错误:“st”未在此范围内声明
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:10:88:错误:应在“int”之前使用主表达式
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:10:97:错误:表达式列表在初始值设定项[-fppermissive]中被视为复合表达式
SteinerTreeObserver::SteinerTreeObserver(ContainerType&ec、STTree&st、int节点)
^
steiner_tree_observer.cpp:11:1:错误:在“{”标记之前应为“,”或“;”{
steiner_tree_observer.cpp:18:25:错误:在“OK”之前应该是初始值设定项。但是我在cpp文件中进行了显式实例化。那么有什么问题吗?–Romerito 3小时前
在哪里进行了显式实例化
您必须将您的cpp包含在标题中(#include“steiner_tree_observer.cpp”)并将其从生成中排除,并从标题中删除“#include”steiner_tree_observer.h”。这是因为模板函数定义在实例化时必须知道,而不像非模板函数/类
顺便说一句,模板类的显式实例化只有在导出该类时才有意义。因此,您可以确定,您的链接器将从您编译的DLL或Lib中删除符号。当您解决编译问题后,读取模板只能在headerfile中定义。好的。但是我在CPP文件中进行了显式实例化。那又如何错误?您好,我发现了问题。:/我没有在某些方法中使用模板关键字,例如:ContainerType&SteinerTreeObserver::get_container(){return m_ec;}
谢谢!!在CPP文件中。模板类SteinerTreeObserver当标头包含时,编译器需要实现。这样的正向声明只有在导出类时才有意义。