gpt4 book ai didi

c++: 无法实例化 std::set 模板类

转载 作者:行者123 更新时间:2023-11-30 00:54:44 25 4
gpt4 key购买 nike

给定以下类(class)

template <typename T>
class Plane
{
public:
Plane<T>(T A, T B, T C, const Vector3<T>& vec):
A(A),
B(B),
C(C),
D(-1*A*vec.x-B*vec.y-C*vec.z)
{

}

Plane<T>():
A(0),
B(0),
C(0),
D(0)
{

}


bool CalculateTime(const Vector3<T>& r0, const Vector3<T>& rd, T& result )
{
Vector3<T> vec(A,B,C);

if ( vec.dot(rd))
{
result = -1;
return false;
}
else
{
result = (vec.dot(r0) + D)/vec.dot(rd);
return true;
}
}

T A;
T B;
T C;
T D;
};

出于某些奇怪的原因,当我执行以下操作时



 std::set
    
     > s;
     
s.insert(Plane (10,20,30,Vector3f(10,30,40)));

这产生了一个巨大的错误

1>          with
1> [
1> T=float
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(2245) : see declaration of 'std::operator <'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(179) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=Plane<float>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(559) : see reference to function template instantiation 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const' being compiled
1> with
1> [
1> _Ty=Plane<float>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(743) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1> with
1> [
1> _Ty=Plane<float>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree(1028) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::less<Plane<float>>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\set(44) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tset_traits<Plane<float>,std::less<Plane<float>>,std::allocator<Plane<float>>,false>
1> ]
1> c:\users\awesome2\google drive\university\eng 3gc3\assignment 3\voxelmodeler\voxelmodeler\voxelcube.h(44) : see reference to class template instantiation 'std::set<_Kty>' being compiled
1> with
1> [
1> _Kty=Plane<float>
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const Plane<T>'
1> with
1> [
1> T=float
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1983) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const Plane<T>'
1> with
1> [
1> T=float
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1259) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const Plane<T>'
1> with
1> [
1> T=float
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1075) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const Plane<T>'
1> with
1> [
1> T=float
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(232) : see declaration of 'std::operator <'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef(180): error C2676: binary '<' : 'const Plane<T>' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> T=float
1> ]

最佳答案

重要的部分在错误消息的末尾:

...
error C2676: binary '<' : 'const Plane<T>' does not define this operator
or a conversion to a type acceptable to the predefined operator

std::set 的元素需要订购即。它需要能够说一个对象小于另一个对象。

最直接的方法是实现一个 operator <你类(class)的方法。不幸的是,定义“小于”对于 3 维平面意味着什么并不那么简单!

使用 std::unordered_set 可能值得研究, 这只要求元素具有可比性。

关于c++: 无法实例化 std::set 模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13613943/

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