C++ 参数化模板方法的显式实例化
我有两个不同的模板类,其中一个使用另一个模板类的对象作为参数。我收到一条错误消息: collect2:ld返回了1个退出状态 make:[main]错误1 我正在处理模板的显式实例化,我发现了一些相关的帖子,但它们大多与单个模板类的实例化有关。在我的例子中,每个类有3个文件(.h、.cpp、.inc,根据建议) 福安C++ 参数化模板方法的显式实例化,c++,templates,class,instantiation,C++,Templates,Class,Instantiation,我有两个不同的模板类,其中一个使用另一个模板类的对象作为参数。我收到一条错误消息: collect2:ld返回了1个退出状态 make:[main]错误1 我正在处理模板的显式实例化,我发现了一些相关的帖子,但它们大多与单个模板类的实例化有关。在我的例子中,每个类有3个文件(.h、.cpp、.inc,根据建议) 福安 我目前还没有一个GCC设置的环境,所以我在这里盲目飞行 您的代码似乎有一些问题(正如我的心智编译器所看到的) 在bar.cpp中,您没有限定doSomething()函数的作用域。
我目前还没有一个GCC设置的环境,所以我在这里盲目飞行 您的代码似乎有一些问题(正如我的心智编译器所看到的)
doSomething()
函数的作用域。如果它已在单独的标题中声明,则需要Bar::doSomething()
。如果条形图是模板化的,您还需要提供一个绑定,即Bar::doSomething()
template template void bar::doSomething(Foo*p){…}
main()。我猜你刚刚错过了new
关键字pFoo=newfoo()代码>
\ifndef BAR\u H
#定义条形图
#包括
模板
分类栏{
公众:。。。
模板
无效剂量测定法(足型*);
二等兵:。。。
};
#恩迪夫
bar.cpp
#包括
#包括
...
...
模板
空白条::剂量测定法(fooType*p){
....
}
.....
#包括“酒吧公司”
酒吧公司
#包括
模板类栏;
模板类栏;
模板空白条::doSomething(Foo*);
模板空白条::doSomething(Foo*);
您好,谢谢您的评论!我编辑了我原来的帖子。您的观察是正确的,事实上,原始源代码已经考虑了您提到的要点。因此,上述三点中的任何一点都不存在缺陷。
#ifndef FOO_H
#define FOO_H
template < class S >
class Foo{
public: ...
private: ...
};
#endif
...
template < class S >
Foo<S>::Foo(){ .... }
...
#include "foo.inc"
template class Foo<int>;
template class Foo<float>;
#ifndef BAR_H
#define BAR_H
#include <mylib.h>
template < class T >
class Bar{
public: ...
template <class S>
void doSomething(Foo<S>*);
private: ...
};
#endif
....
template <class T> template <class S>
void Bar<T>::doSomething(Foo<S>*p){
....
}
.....
#include "bar.inc"
template class Bar<int>;
template class Bar<float>;
template void Bar<int>::doSomething(Foo<int>*);
template void Bar<float>::doSomething(Foo<float>*);
#include <foo.h>
#include <bar.h>
#include <mylib.h>
#include <common.h>
int main(){
Foo<float> * pFoo = NULL;
pFoo = new Foo<float>();
Bar<float> * pBar = NULL;
pBar = new Bar<float>();
pBar->doSomething(pFoo);
delete pFoo;
delete pBar;
return (0);
}
g++ -O3 -Wall main.cpp -o ../bin/main -I../inc -L../lib -lmylib -lm
/tmp/cciJHsKr.o: In function `main':
main.cpp:(.text+0x19f): undefined reference to `void Bar<float>::doSomething<float>(Foo<float>*)'
collect2: ld returned 1 exit status
make: *** [main] Error 1
// Makefile for foo.cpp & bar.cpp
INC=./inc
SRC=./src
LIB=./lib
OBJ=./obj
CC=g++
CFLAGS=-O3 -Wall
mylib: $(LIB)/mylib.a
echo "mylib was created!..."
$(LIB)/mylib.a: \
$(OBJ)/bar.o \
$(OBJ)/foo.o
ar csr $(LIB)/mylib.a \
$(OBJ)/bar.o \
$(OBJ)/foo.o \
$(OBJ)/bar.o: $(SRC)/bar.cpp
$(CC) -c $(CFLAGS) $(SRC)/bar.cpp -I$(INC) \
-o $(OBJ)/bar.o
#ifndef BAR_H
#define BAR_H
#include <mylib.h>
template < class T >
class Bar{
public: ...
template <class fooType>
void doSomething(fooType*);
private: ...
};
#endif
#include <bar.h>
#include <foo.h>
...
...
template <class T> template <class fooType>
void Bar<T>::doSomething(fooType*p){
....
}
.....
#include "bar.inc"
#include <foo.h>
template class Bar<int>;
template class Bar<float>;
template void Bar<int>::doSomething(Foo<int>*);
template void Bar<float>::doSomething(Foo<float>*);