【发布时间】:2017-01-26 14:52:13
【问题描述】:
我有两个要编译的文件。它们如下:
数组.hpp
template<class T> class Array
{
public:
Array() {};
~Array() {};
int width_, height_;
std::vector<T> data_;
template<typename U>
void print(const Array<U>& inputArray);
T* operator()(int x, int y);
}
数组.cpp
template<typename T> template<typename U>
void Array<T>::print(const Array<U>& inputArray)
{
std::cout << ( *inputArray(0, 0) ) << std::endl;
// ERROR: No matching function for call to object of type 'const Array<unsigned char>'
}
template void Array<uint8_t>::print(const Array<uint8_t>& inputArray);
template void Array<uint8_t>::print(const Array<float>& inputArray);
template <typename T>
T* Array<T>::operator()(int x, int y)
{
return &data_[0] + x + y*width_;
}
template uint8_t* Array<uint8_t>::operator()(int x, int y);
template float* Array<float>::operator()(int x, int y);
我完全不清楚为什么对操作员的调用会引发错误。为函数“print”实例化的两种输入类型都明确定义了运算符。 是不是因为编译器先在头文件中查找,找不到指定类型的操作符实例。 在头文件中定义运算符也没有多大用处。这(据我所知)再次引发错误,因为在头文件中实现了两个函数(运算符),名称相同但返回类型不同。
怎么办?
【问题讨论】:
-
ps。我知道代码的实际功能是完全荒谬的。
-
这并没有解决问题,但是
print不能只是一个免费的功能吗?
标签: c++ templates linker-errors template-classes