【问题标题】:C++: error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const SPoint'C++:错误 C2678:二进制“=”:未找到采用“const SPoint”类型的左侧操作数的运算符
【发布时间】:2013-01-07 07:15:51
【问题描述】:

这是我的代码:

#include <StdAfx.h>;
#include <iostream>;
#include <vector>;
#include <algorithm>;
#include <iterator>;

using namespace std;
struct SPoint
{
    int id;
    int X;
    int Y;
};

bool operator<(const SPoint& p1, const SPoint&p2){
    return p1.id <p2.id;
}

vector<SPoint> points;
vector<SPoint> chosen;
vector<SPoint> cleared;

vector<SPoint>::iterator it;

void print_vect(const vector<SPoint> & vect)
{
    for (int i = 0; i < vect.size(); ++i)
    {
        cout << vect[i].id << " (" << vect[i].X << "," << vect[i].Y << ")"<<endl;               
    }           

    cout << endl;   
}
bool compare(double val1, double val2)
{
    return val1 > val2;
}
void sort_points(const vector<SPoint> & vect, char command)
{
    bool cmp_result;
    SPoint temp;
    bool sorted=true;
    for (int i = 0; i < vect.size()-1 ; i++)
    {
        sorted=true;
        for (int j = 1; j <= vect.size()-1; j++)
        {
            switch (command) 
            {
                case 'x': 
                    {
                        cmp_result = compare(vect[j-1].X, vect[j].X); 
                        break;
                    }
                case 'y': 
                    {
                        cmp_result = compare(vect[j-1].Y, vect[j].Y); 
                        break;              
                    }               
                case 'i': 
                    {
                        cmp_result = compare(vect[j-1].id, vect[j].id); 
                        break;              
                    }           
            }

            if (cmp_result)
            {
                sorted = false;
                temp = vect[j-1];
                vect[j-1] = vect[j];
                vect[j] = temp;
            }

        }
        if (sorted) 
        {
            cout << "Sorted:" << endl;
            print_vect(vect);           
            break;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    SPoint temp;
    for (int i = 0; i < 10; i++)
    {
        temp.id = i;
        temp.X = i;
        temp.Y = i;
        points.push_back(temp);
    }

    for (int i = 5; i < 10; i++)
    {
        temp.id = i;
        temp.X = i;
        temp.Y = i;
        chosen.push_back(temp);
    }

    cout << "Points:" << endl;
    print_vect(points);
    cout << endl << endl;

    cout << "Chosen:" << endl;
    print_vect(chosen);

    system("pause");
    sort_points(points, 'i');
    sort_points(chosen, 'i');
    set_difference(points.begin(), points.end(), chosen.begin(), chosen.end(), back_inserter(cleared));
    print_vect(cleared);

    return 0;
}

谁能帮我理解为什么我会出现这些错误:

1>c:\users\гость\documents\visual studio 2010\projects\grablevskij2\grablevskij2\grablevskij2.cpp(110): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const SPoint' (or there is no acceptable conversion)
1>          c:\users\гость\documents\visual studio 2010\projects\grablevskij2\grablevskij2\grablevskij2.cpp(18): could be 'SPoint &SPoint::operator =(const SPoint &)'
1>          while trying to match the argument list '(const SPoint, const SPoint)'
1>c:\users\гость\documents\visual studio 2010\projects\grablevskij2\grablevskij2\grablevskij2.cpp(111): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const SPoint' (or there is no acceptable conversion)
1>          c:\users\гость\documents\visual studio 2010\projects\grablevskij2\grablevskij2\grablevskij2.cpp(18): could be 'SPoint &SPoint::operator =(const SPoint &)'
1>          while trying to match the argument list '(const SPoint, SPoint)'

【问题讨论】:

  • гость,你的例子太长了。尝试将其减少到最低限度。您可以删除此示例中大约 95% 的内容以获得相同的错误。这是一项值得培养的技能,可以帮助您自助。

标签: c++


【解决方案1】:

在您的sort_points 函数中,您将vect 声明为const。后来,你说:

vect[j-1] = vect[j];
vect[j] = temp;

由于vect 是const,这会导致operator[] 的const 版本被调用,这会返回对对象的const 引用,该引用不可分配。

鉴于它是如何命名的 sort_points 并且不返回新向量,我希望它会修改我传入的向量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    相关资源
    最近更新 更多