c++ - 使用赋值运算符时 C++ 中的编译错误 =?

标签 c++ visual-studio oop compiler-errors

我写了一个程序,这是作为家庭作业给我的(有点长)。问题是它在 CodeBlocks 中编译,但在 Visual Studio 2017 中不编译,它说 - binary '=': no operator found which takes a right- 'CAutomobile' 类型的手操作数(或者没有可接受的转换。 请问为什么是因为我自己找不到错误?我尝试评论运算符 =function 但错误仍然存​​在。

#include <iostream>
#include <algorithm>
#include <string>
#include <stdlib.h>
using namespace std;
class CVehicle {
    string name;
    int year;
public:
    CVehicle() {
        name = "Car";
        year = 1990;
    }
    CVehicle(string n, int y) {
        name = n;
        year = y;
    }
    CVehicle(const CVehicle& vc) {
        name = vc.name;
        year = vc.year;
    }
    void setName(string n) {
        name = n;
    }
    void setYear(int y) {
        year = y;
    }
    string getName() {
        return name;
    }
    int& getYear() {
        return year;
    }
    virtual void Print(ostream& os) = 0;
};
class CAutomobile :public CVehicle {
    double litres;
public:
    CAutomobile() :CVehicle() {
        litres = 7.2;
    }
    CAutomobile(string nm, int yr, double l) :CVehicle(nm, yr) {
        litres = l;
    }
    void setLitres(double l) {
        l = litres;
    }
    double& getLitres() {
        return litres;
    }
    void Print(ostream& os) override {
        os << getName() << endl;
        os << getYear() << endl;
        os << litres << endl;
    }
    friend bool operator< (CAutomobile a1, CAutomobile a2) {
        if (a1.litres < a2.litres) {
            return true;
        }
        return false;
    }
    CAutomobile operator= (CAutomobile&  at) {
        CAutomobile au;
        au.getName() = at.getName();
        au.getYear() = at.getYear();
        au.getLitres() = at.getLitres();
        return au;
    }
    CAutomobile operator+(CAutomobile aut) {
        CAutomobile a;
        a.getLitres() = getLitres() + aut.getLitres();
        return a;
    }
    friend ostream& operator<< (ostream& o, CAutomobile a) {
        o << a.getName() << endl;
        o << a.getYear() << endl;
        o << a.getLitres() << endl;
        return o;
    }
};
int main()
{
    CAutomobile a[] = {
    CAutomobile(),
    CAutomobile("Wolkswagen",1970,80.5),
    CAutomobile("Fiat",1979,21.9),
    CAutomobile("Opel",1978,13.7)
    };
    for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
        cout << "Name" << ' ' << a[i].getName() << endl;
        cout << "Year" << ' ' << a[i].getYear() << endl;
        cout << "Litres" << ' ' << a[i].getLitres() << endl;
    }
    int range = 2016 - 1990 + 1;
    for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
        a[i].setLitres(rand() % 100 + 1);
        a[i].setYear(rand() % range + 1996);
    }
    //сортираме масива по литри и извеждаме 
    //най малкия (първия) му елемент
    for (int i = 0; i < sizeof(a-1); i++) {
        for (int j = 0; j < sizeof(a-1); j++) {
            if (a[j].getLitres() > a[j + 1].getLitres()) {
                swap(a[j], a[j + 1]);
            }
        }
    }   
    cout << a[0] << endl;
    CAutomobile k = a[0] + a[3];
    cout << k.getLitres() << endl;
}

最佳答案

CAutomobile::operator = 是完全错误的。它采用非常量引用并将其字段分配给新对象。相反,它应该采用 const 引用并修改当前对象。

CAutomobile & operator =(CAutomobile const & other)
{
    assert(this != ::std::addressof(other)); // check for self-assignment
    SetName(other.getName());
    SetYear(other.getYear());
    SetLitres(other.getLitres());
    return *this;
}

这会带来另一个问题:getter 不是 const 限定的,所以它们也应该被修复:

string const & getName(void) const {
    return name;
}
int const & getYear(void) const {
    return year;
}

关于c++ - 使用赋值运算符时 C++ 中的编译错误 =?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53201658/

相关文章:

c++ - 在 Makefile 中查找 Boost 库

c++ - 如何以编程方式在控制台应用程序中选择文本?

javascript - 如何让 Logo 出现在滚动的粘性菜单中

java - Java中将对象转换为其子类

c++ - 没有继承的公共(public)成员

c++ - Visual Studio 2015 C2011 'Class' 类型重定义

c++ - 为什么将整数分配给字符串会导致空白?

visual-studio - 我怎样才能让 bjam 在其他目录中找到我的 .h 文件

c++ - 在 windows 中包含 .h 路径,但在 visual studio 中忽略 unix

oop - 将面向对象模型映射到 Clojure