我已经为一个非常简单的打印函数编写了一个模板,并且将它放在了一个所有处理控制台IO的自定义函数库中。在单独的.cpp文件中专门设计函数模板
我已经为一个特定的项目编写了第二个库,那是一个原创的小孩。它专门化了其中一个模板。
我遇到了一个错误(我怀疑)是由main.cpp中的调用在专用模板声明之前发生的。错误有这样一行:
In instantiation of 'static void baseIO::print(S) [with s = std::vector<int>]'
这意味着它的调用的specialIO::print()
baseIO::print()
代替
我试图使specialIO::print()
普通函数,而不是一个模板,并在头宣布它作为正常的,但否认主.cpp访问基本模板。
有没有办法让我的专业化在main.cpp中可用而无需
宣布
在那里实现呢?
//in main.cpp
#include <vector>
#include "specialIO.h"
main(){
std::vector<int> myVector;
specialIO::print(myVector);
specialIO::print("hello world");
return 1;
}
。
//in baseIO_templates.cpp - templates are outside of the baseIO.cpp file because of linker errors
template<typename S> //primary template
void baseIO::print(S str){
std::cout << str;
}
//baseIO.h
class baseIO{
public:
template<typename S> //primary template
static void print(S str);
}
#include "baseIO_templates.cpp"
。
//specialIO.cpp
template<> //specialized template
void static specialIO::print(vector<int> myVector){
for(int i : myVector){
baseIO::print(i)
}
}
//specialIO.h
class uberIO : public baseIO {
//empty
}
任何特定的原因,为什么你总是复制矢量,而不是通过const ref传递它? – axalis