参数化模板方法的显式实例化 [英] explicit instantiation of parameterized template methods
问题描述
我有两个不同的模板类,其中一个使用另一个模板类的对象作为参数.我收到一条错误消息: collect2:ld返回1退出状态 make: * [main]错误1
I've two different template classes, one of them using as parameter an object of the another template class. i'm getting an error message: collect2: ld returned 1 exit status make: * [main] Error 1
我正在使用模板的显式实例化,我发现了一些相关的文章,但它们大多与单个模板类的实例化有关.就我而言,我每个班级有3个文件(.h,.cpp,.inc,紧随 http://www.parashift.com/c++-faq/templates.html#faq-35.13 建议).
I'm working with explicit instantiation of templates and I found some related posts but they were mostly related to instantiations of one single template class. In my case, I've 3 files per class (.h, .cpp, .inc following http://www.parashift.com/c++-faq/templates.html#faq-35.13 suggestion).
#ifndef FOO_H
#define FOO_H
template < class S >
class Foo{
public: ...
private: ...
};
#endif
foo.cpp
...
template < class S >
Foo<S>::Foo(){ .... }
...
#include "foo.inc"
foo.inc
template class Foo<int>;
template class Foo<float>;
bar.h
#ifndef BAR_H
#define BAR_H
#include <mylib.h>
template < class T >
class Bar{
public: ...
template <class S>
void doSomething(Foo<S>*);
private: ...
};
#endif
bar.cpp
....
template <class T> template <class S>
void Bar<T>::doSomething(Foo<S>*p){
....
}
.....
#include "bar.inc"
bar.inc
template class Bar<int>;
template class Bar<float>;
template void Bar<int>::doSomething(Foo<int>*);
template void Bar<float>::doSomething(Foo<float>*);
mylib.h
#include <foo.h>
#include <bar.h>
main.cpp
#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);
}
有人在实例化对象的方式上看到任何问题/错误吗?完整的错误消息是(我更改了类的名称并简化了代码,以便于理解):
Does anybody see any problem/bug on the way I'm instantiating the objects? The complete error message is (I changed the name of the classes and simplified the code so as to be more understandable):
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
请注意,由于我正在生成库,因此我分别编译了foo.cpp和bar.cpp.
Note that, I compile both foo.cpp and bar.cpp separately, since I'm generating a library.
// 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
bar.cpp
#include <bar.h>
#include <foo.h>
...
...
template <class T> template <class fooType>
void Bar<T>::doSomething(fooType*p){
....
}
.....
#include "bar.inc"
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>*);
这篇关于参数化模板方法的显式实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!