[英]error C2677: binary '+=' : no global operator found which takes type 'Movie' (or there is no acceptable conversion)
I'm trying to create a generic class which stores Movie
objects in an array.我正在尝试创建一个将
Movie
对象存储在数组中的通用类。 I want to override the +=
and -=
operators in order to add or delete an element from the array.我想覆盖
+=
和-=
运算符,以便从数组中添加或删除元素。
template<typename Element>
movieArray<Element>& movieArray<Element>::operator-=(int position)
{
// some code here...
return *this;
}
template<typename Element>
void movieArray<Element>::deleteElem(int position)
{
this->elements -= position;
}
The delete part works as expected.删除部分按预期工作。 However, when I do the same thing for the Add operation:
但是,当我为 Add 操作做同样的事情时:
template<typename Element>
movieArray<Element>& movieArray<Element>::operator+=(const Element &elem)
{
elements[lenght++] = elem;
return *this;
}
template<typename Element>
void movieArray<Element>::addElem(const Element &elem)
{
resizeArray();
this->elements += elem;
}
I receive the following error:我收到以下错误:
error C2677: binary '+=' : no global operator found which takes type 'Movie' (or there is no acceptable conversion)
错误 C2677:二进制“+=”:未找到采用“电影”类型的全局运算符(或没有可接受的转换)
I am not sure why this is happening.我不确定为什么会这样。
If I change Element
to int
, like I have at the -=
method, the code is compiled.如果我将
Element
更改为int
,就像我在-=
方法中int
那样,代码将被编译。
Try this instead:试试这个:
template<typename Element>
movieArray<Element>& movieArray<Element>::operator-=(int position)
{
// some code here...
return *this;
}
template<typename Element>
void movieArray<Element>::deleteElem(int position)
{
*this -= position;
}
template<typename Element>
movieArray<Element>& movieArray<Element>::operator+=(const Element &elem)
{
resizeArray();
elements[lenght++] = elem;
return *this;
}
template<typename Element>
void movieArray<Element>::addElem(const Element &elem)
{
*this += elem;
}
Or this:或这个:
template<typename Element>
movieArray<Element>& movieArray<Element>::operator-=(int position)
{
deleteElem(position);
}
template<typename Element>
void movieArray<Element>::deleteElem(int position)
{
// some code here...
}
template<typename Element>
movieArray<Element>& movieArray<Element>::operator+=(const Element &elem)
{
addElem(elem);
return *this;
}
template<typename Element>
void movieArray<Element>::addElem(const Element &elem)
{
resizeArray();
elements[lenght++] = elem;
}
We are guessing a lot since you only posted a little fraction of your code.我们猜测很多,因为您只发布了一小部分代码。 But perhaps you meant:
但也许你的意思是:
*this += elem;
which would call your class's overloaded operator+=
function that you showed, instead of:这将调用您显示的类的重载
operator+=
函数,而不是:
this->elements += elem;
which would call +=
on the member variable elements
.这将在成员变量
elements
上调用+=
。 You haven't told us what elements
is , but (based on your symptoms) perhaps it has some type which has +=
defined for int
but not for Element
.您还没有告诉我们
elements
是什么,但是(根据您的症状)也许它具有某种类型,它为int
定义了+=
而不是为Element
定义的。
NB.注意。 Not related to your compilation error, but
resizeArray();
与您的编译错误无关,而是
resizeArray();
should be inside operator+=
, otherwise (assuming that this function is necessary to make space in the array) you will overflow your space when someone calls operator+=
instead of addElem
.应该在
operator+=
,否则(假设此函数对于在数组中腾出空间是必要的)当有人调用operator+=
而不是addElem
时,您将溢出您的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.