Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ VC++;Express 2010在模板类的重载赋值运算符的自赋值测试中出错_C++_Overloading_Operator Keyword - Fatal编程技术网

C++ VC++;Express 2010在模板类的重载赋值运算符的自赋值测试中出错

C++ VC++;Express 2010在模板类的重载赋值运算符的自赋值测试中出错,c++,overloading,operator-keyword,C++,Overloading,Operator Keyword,我试图重写赋值运算符并执行自赋值测试,但VC++Express 2010为我的代码提供了以下错误,如下所示: 1> c:\users\fatak\documents\visual studio 2010\projects\ray tracer\ray tracer\test.h(22):错误C2440:“==”:无法从“常量测试*”转换为“测试*常量” #ifndef __TEST_H__ #define __TEST_H__ template <class T = unsigned i

我试图重写赋值运算符并执行自赋值测试,但VC++Express 2010为我的代码提供了以下错误,如下所示:

1> c:\users\fatak\documents\visual studio 2010\projects\ray tracer\ray tracer\test.h(22):错误C2440:“==”:无法从“常量测试*”转换为“测试*常量”

#ifndef __TEST_H__
#define __TEST_H__

template <class T = unsigned int> class Test
{
public:
Test() : dummy(0U) {};
template <class U> Test(U value) : dummy(T(value)) {};
~Test() {};

template <class U> Test<T> &operator=(const Test<U> &rhs);

T getValue(void) const {return dummy;};

template <class U> friend class Test;
private:
T dummy;
};

template <class T> template <class U> Test<T> &Test<T>::operator=(const Test<U> &rhs)
{
if(this == &rhs)
    return *this;

dummy = T(rhs.dummy);

return *this;
}

#endif //__TEST_H__
\ifndef\uu测试__
#定义测试__
模板类测试
{
公众:
Test():虚拟(0U){};
模板测试(U值):虚拟(T(值)){};
~Test(){};
模板测试和操作员=(常量测试和rhs);
T getValue(void)const{return dummy;};
模板类测试;
私人:
T型假人;
};
模板测试和测试::运算符=(常量测试和rhs)
{
如果(此==&rhs)
归还*这个;
假人=T(右侧假人);
归还*这个;
}
#endif/\uu测试__
即使将模板化重写赋值运算符的操作数更改为:

template <class T> template <class U> Test<T> &Test<T>::operator=(Test<U> & const rhs)
模板测试和测试::运算符=(测试和常量rhs)
我得到以下错误:

1> c:\users\fatak\documents\visual studio 2010\projects\ray tracer\ray tracer\test.h(22):错误C2440:“==”:无法从“测试*”转换为“测试*常量”

#ifndef __TEST_H__
#define __TEST_H__

template <class T = unsigned int> class Test
{
public:
Test() : dummy(0U) {};
template <class U> Test(U value) : dummy(T(value)) {};
~Test() {};

template <class U> Test<T> &operator=(const Test<U> &rhs);

T getValue(void) const {return dummy;};

template <class U> friend class Test;
private:
T dummy;
};

template <class T> template <class U> Test<T> &Test<T>::operator=(const Test<U> &rhs)
{
if(this == &rhs)
    return *this;

dummy = T(rhs.dummy);

return *this;
}

#endif //__TEST_H__
知道为什么吗?或者我怎样才能对任何人进行成功的自我分配测试

干杯

类型
Test
Test
是完全不同的类型(除非T是U)

不能将指针与不相关的类型进行比较。而且它们无论如何都不可能是相同的,因为不相关的类型不能存在于同一地址


您可能应该有一个非模板的
操作符=(const Test&)
,如果需要,它可以测试自赋值,还有一个
操作符=(const Test&)
,它不需要测试