c ++错误:operator []:2重载具有类似的转换 [英] c++ error: operator []: 2 overloads have similar conversions

查看:267
本文介绍了c ++错误:operator []:2重载具有类似的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  template< typename T> 
class v3 {
private:
T _a [3];

public:
T& operator [](unsigned int i){return _a [i]; }
const T& operator [](unsigned int i)const {return _a [i]; }

operator T *(){return _a; }
operator const T *()const {return _a; }

v3(){
_a [0] = 0; // works
_a [1] = 0;
_a [2] = 0;
}

v3(const v3< T& v){
_a [0] = v [0] //错误1错误C2666:'v3< T> :: operator []':2重载具有类似的转换
_a [1] = v [1] //错误2错误C2666:'v3< T> :: operator []':2重载具有类似的转换
_a [2] = v [2] //错误3错误C2666:'v3< T> :: operator []':2重载具有类似的转换
}
};

int main(int argc,char ** argv)
{
v3< float> v1;
v3< float> v2(v1);

return 0;
}


解决方案

错误消息(在输出窗口中),它变得更清楚:

  1>可以是'const float& v3< T> :: operator [](unsigned int)const'
1> with
1> [
1> T = float
1> ]
1>或'内置C ++运算符[(const float *,int)'
1>同时试图匹配参数列表'(const v3< T>,int)'
1> with
1> [
1> T = float
1> ]

编译器无法决定是否使用重载的 或者在 const T * 上的内置 operator [] 通过以下转换函数:

  operator const T *()const {return _a; } 

以下两个对潜在行的潜在有效解释:

  v.operator float *()[0] 
v.operator [](0)



您可以通过显式转换无符号整数索引来删除歧义,以便不需要转换:

  _a [0] = v [static_cast< unsigned int>(0)]; 

或通过更改重载的 operator [] s取出 int 而不是 unsigned int ,或删除运算符T * ()const (也可能是非const版本,为了完整性)。


template <typename T>
class v3 {
private:
    T _a[3];

public:
    T & operator [] (unsigned int i) { return _a[i]; }
    const T & operator [] (unsigned int i) const { return _a[i]; }

    operator T * () { return _a; }
    operator const T * () const { return _a; }

    v3() {
    	_a[0] = 0; // works
    	_a[1] = 0;
    	_a[2] = 0;
    }

    v3(const v3<T> & v) {
    	_a[0] = v[0]; // Error	1	error C2666: 'v3<T>::operator []' : 2 overloads have similar conversions
    	_a[1] = v[1]; // Error	2	error C2666: 'v3<T>::operator []' : 2 overloads have similar conversions
    	_a[2] = v[2]; // Error	3	error C2666: 'v3<T>::operator []' : 2 overloads have similar conversions
    }
};

int main(int argc, char ** argv)
{
    v3<float> v1;
    v3<float> v2(v1);

    return 0;
}

解决方案

If you read the rest of the error message (in the output window), it becomes a bit clearer:

1>        could be 'const float &v3<T>::operator [](unsigned int) const'
1>        with
1>        [
1>            T=float
1>        ]
1>        or       'built-in C++ operator[(const float *, int)'
1>        while trying to match the argument list '(const v3<T>, int)'
1>        with
1>        [
1>            T=float
1>        ]

The compiler can't decide whether to use your overloaded operator[] or the built-in operator[] on the const T* that it can obtain by the following conversion function:

operator const T * () const { return _a; }

Both of the following are potentially valid interpretations of the offending lines:

v.operator float*()[0]
v.operator[](0)

You can remove the ambiguity by explicitly casting the integer indices to be unsigned so that no conversion is needed:

_a[0] = v[static_cast<unsigned int>(0)];

or by changing your overloaded operator[]s to take an int instead of an unsigned int, or by removing the operator T*() const (and probably the non-const version too, for completeness).

这篇关于c ++错误:operator []:2重载具有类似的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆