gpt4 book ai didi

c++ - operator= 从模板类重载

转载 作者:行者123 更新时间:2023-11-28 00:21:02 24 4
gpt4 key购买 nike

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

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

在我应用模板之前它工作正常。但是随后出现了一些错误。

据我所知,我需要缺少和 operator= 重载来对值进行类型转换,例如,我有一个 float 并且我正在接收一个 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)

最佳答案

在成员声明中你有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;
}

关于c++ - operator= 从模板类重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com