简体   繁体   English

错误 C2677:二进制“+=”:未找到采用“电影”类型的全局运算符(或没有可接受的转换)

[英]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.

相关问题 运算符重载:简单加法...错误C2677:二进制&#39;+&#39;:找不到类型为___的全局运算符(或者没有可接受的转换) - Operator Overloading: Simple Addition… error C2677: binary '+': no global operator found with takes type ___ (or there is not acceptable conversion) 错误 C2677:二进制“*”:未找到全局运算符 - Error C2677: binary '*': no global operator found 二进制&#39;*&#39;:找不到全局运算符,它接受类型&#39;统计者&#39;(或者没有可接受的转换) - binary '*' : no global operator found which takes type 'statistician' (or there is no acceptable conversion) 错误C2678:二进制'==':找不到哪个运算符采用类型的左操作数(或者没有可接受的转换) - error C2678: binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion) 错误C2678:“ ==”二进制文件:未找到采用“ CSchemaString”类型的左操作数的运算符(或没有可接受的转换) - error C2678: '==' binary: no operator found which takes a left operand of type 'CSchemaString' (or there is no acceptable conversion) 运算符重载:用二进制“-”减法没有找到采用_____类型的全局运算符(或没有可接受的转换) - Operator Overloading: subtraction with binary '-' no global operator found which takes type _____ (or there is no acceptable conversion) 枚举类型错误C2677 - Enum type error C2677 错误2错误C2679:二进制&#39;/&#39;:找不到哪个操作符采用类型的右操作数(或者没有可接受的转换) - Error 2 error C2679: binary '/' : no operator found which takes a right-hand operand of type (or there is no acceptable conversion) 错误 C2679:二进制“&lt;&lt;”:未找到采用“mystring”类型的右侧操作数的运算符(或没有可接受的转换) - error C2679: binary '<<': no operator found which takes a right-hand operand of type 'mystring' (or there is no acceptable conversion) 错误C2679:二进制&#39;&gt;&#39;:找不到使用类型为&#39;int&#39;的右侧操作数的运算符(或没有可接受的转换) - error C2679: binary '>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM