繁体   English   中英

运算符重载:用二进制“-”减法没有找到采用_____类型的全局运算符(或没有可接受的转换)

[英]Operator Overloading: subtraction with binary '-' no global operator found which takes type _____ (or there is no acceptable conversion)

我正在尝试编写一个代码,通过使用 eof() 从文件中读取来计算注册费。 但是,当我尝试编译时,我得到一个错误,即 C2677,这意味着二进制“-”没有找到全局运算符。 我已经研究过如何解决它,但我不明白。 这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;

    string VIN, make, model, type, year;
    string truck = "TRUCK";
    double basefee, weight;
    double tax = 0.065;
    double highwayfund = 2.0;
    int age;
    double discount;

    inFile.open("VehicleInput.txt");
    outFile.open("VehicleOutput.txt");

    while (!inFile.eof())
    {
        inFile >> VIN >> make >> model >> type >> year;
        inFile >> weight;
        age = (2020 - year);
        weight = 12000;
        discount = (age * 0.1);

        if (age >= 7)
        {
            discount = 0.7;
        }

        if ((type == "CAR") || (type == "SUV"))
        {
            basefee = (100 - ((100.0 * tax) - (100 * discount)) + highwayfund);
        }
        if (type == "BUS")
        {
            basefee = (200 - ((200.0 * tax) - (200 * discount)) + highwayfund);
        }
        if (type == "TRUCK")
        {
            basefee = (500 - ((500.0 * (0.22 + tax)) - (500 * discount)) + highwayfund);
        }


        outFile << " " << VIN << " " << make << " " << model << " " << year;
        if (weight > 12000) outFile << weight;
        outFile << "$ " << basefee << endl;


    }
    return 0;
}

问题是这一行:

age = (2020 - year);

age是一个int 2020是一个整数。 您声明为字符串的year

string VIN, make, model, type, year;
int age;

这正是错误告诉你的。 没有operator-接受intstring

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM