operator =从模板化类中重载

时间:2014-12-14 18:28:19

标签: c++ templates casting operator-overloading

我有一个项目将我的所有类模板化为int,double和float,getCoordinate返回一个CCoordinate类型的对象。

            tempCoordinate = m_shapes.at(i)->getCoordinate(j);

在我应用模板之前,它正常运行。但是出现了一些错误。

根据我的理解,我需要我缺少和operator = overload来对类型进行类型转换,例如我有一个浮点数并且我收到一个int,例如:

        CCoordinate<float> coorFloat;
        CCoordinate<int> coorInt = coorFloat

我如何在课堂上创建这个?它需要什么格式?

我觉得它应该是这样的,但显然我错了。

//CCoordinate.h
template<class T>
class CCoordinate {
 //Code
 public:
 template<class U> template <class U> CCoordinate<T>
            operator= (const CCoordinate<U>& c1);
}

//CCoordinate.cpp
template <class U >
CCoordinate<U> CCoordinate<T>::operator= (const CCoordinate<U>& c1)
{
    // some kind of casting ? 
}

我的错误:

19:06:43 **** Incremental Build of configuration Debug for project ShapesRefV2 ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -Werror=return-type -o "myCode\\CRectangle.o"      "..\\myCode\\CRectangle.cpp" 
g++ -O0 -g3 -Wall -c -fmessage-length=0 -Werror=return-type -o "myCode\\CPlane.o"    "..\\myCode\\CPlane.cpp" 
..\myCode\CPlane.cpp: In instantiation of 'GraSys::CRectangle<T>       GraSys::CPlane<T>::boundingBox(std::string, std::string) [with T = int; std::string =    std::basic_string<char>]':
..\myCode\CPlane.cpp:165:24:   required from here
..\myCode\CPlane.cpp:115:20: error: no match for 'operator=' (operand types are   'GraSys::CCoordinate<double>' and 'const GraSys::CCoordinate<int>')
  tempCoordinate = m_shapes.at(i)->getCoordinate(j);
                ^
..\myCode\CPlane.cpp:115:20: note: candidate is:
In file included from ..\myCode\CGraphicElement.h:14:0,
               from ..\myCode\CPlane.h:11,
               from ..\myCode\CPlane.cpp:9:
..\myCode\CCoordinate.h:17:7: note: GraSys::CCoordinate<double>&   GraSys::CCoordinate<double>::operator=(const GraSys::CCoordinate<double>&)
class CCoordinate
        ^
..\myCode\CCoordinate.h:17:7: note:   no known conversion for argument 1 from 'const    GraSys::CCoordinate<int>' to 'const GraSys::CCoordinate<double>&'
..\myCode\CPlane.cpp: In instantiation of 'GraSys::CRectangle<T> GraSys::CPlane<T>::boundingBox(std::string, std::string) [with T = float; std::string =   std::basic_string<char>]':
..\myCode\CPlane.cpp:166:24:   required from here
..\myCode\CPlane.cpp:115:20: error: no match for 'operator=' (operand types are 'GraSys::CCoordinate<double>' and 'const GraSys::CCoordinate<float>')
     tempCoordinate = m_shapes.at(i)->getCoordinate(j);
                    ^
..\myCode\CPlane.cpp:115:20: note: candidate is:
In file included from ..\myCode\CGraphicElement.h:14:0,
                 from ..\myCode\CPlane.h:11,
                 from ..\myCode\CPlane.cpp:9:
..\myCode\CCoordinate.h:17:7: note: GraSys::CCoordinate<double>&   GraSys::CCoordinate<double>::operator=(const GraSys::CCoordinate<double>&)
 class CCoordinate
       ^
..\myCode\CCoordinate.h:17:7: note:   no known conversion for argument 1 from 'const GraSys::CCoordinate<float>' to 'const GraSys::CCoordinate<double>&'

19:06:44 Build Finished (took 674ms)

2 个答案:

答案 0 :(得分:3)

在会员声明中,template <class U>次数过多,会员应该返回对*this的引用,因此需要返回CCordinate &<T>是如果省略则表示暗示):

// Remove this       vvvvvvvvvvvvvvvvvv
template<class U> /* template <class U> */
CCoordinate & operator= (const CCoordinate<U>& c1);
//          ^- Return type changed to be a reference.

由于成员是模板而类是模板,因此您有两个级别的模板。实现成员时需要指定两个级别。

它也返回了错误的类型(它返回CCoordinate<U>,但你已声明它在类中返回CCoordinate<T>

// You need the T template as well.
// vvvvvvvvvvvvvvv
template <class T>
template <class U>
CCoordinate<T> & CCoordinate<T>::operator= (const CCoordinate<U>& c1)
//          ^  ^- Added reference as per above.
//          \---- Changed to T; U makes no sense here and conflicts with your member
//                declaration in the class.
{
    // Your logic to make the conversion.

    return *this;
}

答案 1 :(得分:0)

您的尝试有两个问题。更简单的问题是operator=的声明语法有一个额外的template <class U>。它应该是这样的:

template<class U> CCoordinate<T>
            operator= (const CCoordinate<U>& c1);

但是,即使是正确定义的operator=也不允许您编写

CCoordinate<float> coorFloat;
CCoordinate<int> coorInt = coorFloat;

这是因为copy initializes coorInt上方的第二行。 operator=不考虑复制初始化 - 它只查看user-defined conversions,在这种情况下只包含非显式构造函数和非显式转换函数。