下面的示例定义了一个基本的podtype容器类.然后使用此类创建一系列typedef,它们表示基本podtype的OOP版本.当我们开始将这些类型分配给彼此时,问题就产生了.
我尝试将运算符定义为使用lhs和rhs参数的友元方法,使用普通的PodObjects作为类型,但没有任何成功.有没有人可能经历过类似的事情或知道这个问题的其他解决方案.
提前致谢.
#include <stdint.h>
template <typename T>
class PodObject {
protected:
T _value;
public:
PodObject<T>(int rhs) {
this->_value = static_cast<T>(rhs);
}
PodObject<T> operator+= (PodObject<T> const &rhs){
this->_value = rhs._value;
return *this;
}
};
typedef PodObject<int8_t> Int8;
typedef PodObject<int16_t> Int16;
int main() {
Int16 a = 10;
Int8 b = 15;
a += b; // Source of problem
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果在编译器输出中:
example.cpp:26:11: error: no viable overloaded '+='
a += b;
~ ^ ~
example.cpp:13:22: note: candidate function not viable: …Run Code Online (Sandbox Code Playgroud)