我有一個類模板,這是簡化的,有點像:如何特化類模板成員函數?
template<typename T>
class A
{
protected:
T _data;
public:
A* operator%(const A &a2) const
{
A * ptr;
ptr = new A(this->_data % a2._data);
return ptr;
}
};
並且從這個類繼承了另一個類:
class B : public A<double>
{
// ...
};
但是,當我這樣做,編譯器說: :
invalid operands of types ‘double’ and ‘const double’ to binary ‘operator%’
然後,我想我專門爲operator%
雙和float因爲這些類型似乎不可能%。我在A類聲明後添加了以下代碼。
template<>
A* A<double>::operator%(const A &a2) const
{
A * ptr;
ptr = new A((uint32_t)this->_data % (uint32_t)a2._data);
return ptr;
}
而且我得到這個錯誤,我真的不明白爲什麼...
In function `A<double>::operator%(A const&) const':
./include/A.hpp:102: multiple definition of `A<float>::operator%(A const&) const'
src/Processor.o:./include/A.hpp:102: first defined here
謝謝你,它的作品就像一個魅力! – 2013-02-14 10:30:43