【问题标题】:error in type conversion in operator overloading运算符重载中的类型转换错误
【发布时间】:2013-11-04 17:28:10
【问题描述】:

我有一个模板类,我需要重载运算符 ==。我通过以下方式做到这一点

template <typename T>
class Polynomial {
    vector<T> coefficients;

    public:
    Polynomial(vector<T> c);

    bool operator ==(const Polynomial& second) const {
            const typename vector<T>::iterator thisBegin = this->coefficients.begin();
            const typename vector<T>::iterator secondBegin = second.coefficients.begin();
            for ( ; ((thisBegin != this->coefficients.end()) &&
                                    (secondBegin != second.coefficients.end()));
                            ++thisBegin, ++secondBegin) {
                    if (*thisBegin != *secondBegin)
                            return false;
            }
            while (thisBegin != this->coefficients.end()) {
                    if (*thisBegin != 0)
                            return false;
                    ++thisBegin;
            }
            while (secondBegin != second.coefficients.end()) {
                    if (*secondBegin != 0)
                            return false;
                    ++secondBegin;
            }
            return true;
    }
};

但是,当我使用 T=int 创建此类的两个对象并尝试应用此运算符时

Polynomial<int> first(firstVector);
Polynomial<int> second(secondVector);
std::cout << (first == second) << std::endl;

我收到了错误

problem2.cpp: In instantiation of ‘bool Polynomial<T>::operator==(const Polynomial<T>&)    const [with T = int; Polynomial<T> = Polynomial<int>]’:
problem2.cpp:63:32:   required from here
problem2.cpp:23:83: error: conversion from ‘std::vector<int, std::allocator<int> >::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >}’ to non-scalar type ‘std::vector<int, std::allocator<int> >::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >}’ requested

有人能指出这种转换有什么问题吗?谢谢!

【问题讨论】:

    标签: c++ operator-overloading


    【解决方案1】:

    您正在尝试将const_iterator 转换为iterator

    const typename vector<T>::iterator thisBegin = this->coefficients.begin();
    

    this 在这种情况下是const,所以this-&gt;coefficients.begin(); 返回一个const_iterator。试试这个:

    typename vector<T>::const_iterator thisBegin = this->coefficients.begin();
    

    另请注意,thisBegin 不是 const,如您的示例所示。这是因为您随后会做这种事情:

    ++secondBegin;
    

    这要求const_iterator 是非常量的(这意味着您可以修改迭代器,但不能修改它指向的东西)。

    【讨论】:

      【解决方案2】:
      • 您的方法是 const,这意味着您只能在 this
      • 上调用 const 函数
      • 您将const 引用传递给方法,因此您只能在其上调用const 函数

      所以,两个

       this->coefficients.begin();
       second.coefficients.begin()
      

      返回常量迭代器。

      您不能将它们分配给非const 的。

      有一个解决办法:

      vector<T>::const_iterator& thisBegin = this->coefficients.begin();
      vector<T>::const_iterator& secondBegin = second.coefficients.begin();
      

      (使用对const_iterator的引用)

      更好:

      auto& thisBegin = this->coefficients.begin();
      auto& secondBegin = second.coefficients.begin();
      

      (使用对auto的引用,C++11 特性)

      顺便说一句,您可以使用std::mismatch 简单地比较两个向量

      【讨论】:

      • 你的例子给你留下了悬而未决的参考。
      • @juanchopanza 哎呀!在哪里? =)
      • vector&lt;T&gt;::const_iterator&amp; thisBegin = this-&gt;coefficients.begin(); 和其他人。
      • @juanchopanza 好的,我猜到了,但为什么呢?
      • 因为begin()end() 返回右值。它们是临时的,所以你必须复制它们。
      猜你喜欢
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      • 2015-12-24
      • 2011-01-15
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      相关资源
      最近更新 更多