简体   繁体   English

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

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

I'm trying to write a code that calculates the registration fee by reading from a file using eof().我正在尝试编写一个代码,通过使用 eof() 从文件中读取来计算注册费。 However, when I try to compile I get one error, which is C2677, which means that binary "-" no global operator found.但是,当我尝试编译时,我得到一个错误,即 C2677,这意味着二进制“-”没有找到全局运算符。 I've looked into how to fix it, but I'm not understanding.我已经研究过如何解决它,但我不明白。 Here is my code:这是我的代码:

#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;
}

The problem is this line:问题是这一行:

age = (2020 - year);

age is an int . age是一个int 2020 is an int. 2020是一个整数。 year you declared as a string.您声明为字符串的year

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

That's exactly what the error is telling you.这正是错误告诉你的。 There is no operator- which takes an int and string .没有operator-接受intstring

暂无
暂无

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

相关问题 二进制&#39;*&#39;:找不到全局运算符,它接受类型&#39;统计者&#39;(或者没有可接受的转换) - binary '*' : no global operator found which takes type 'statistician' (or there is no acceptable conversion) 运算符重载:简单加法...错误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 which takes type 'Movie' (or there is no acceptable conversion) 重载插入运算符:未找到采用“ unsigned int”类型的右侧操作数的运算符(或者没有可接受的转换) - Overloading Insertion Operator: no operator found which takes a right-hand operand of type 'unsigned int' (or there is no acceptable conversion) 二进制&#39;operator&#39;:未找到采用&#39;Fraction&#39;类型的右侧操作数的运算符(或没有可接受的转换) - Binary 'operator' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion) C2678二进制&#39;==&#39;:找不到使用&#39;Card&#39;类型的左操作数的运算符(或者没有可接受的转换) - C2678 binary '==': no operator found which takes a left-hand operand of type 'Card' (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) binary&#39;==&#39;:找不到带有&#39;std :: string&#39;类型的左手操作数的运算符(或者没有可接受的转换) - binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion) 二进制“==”:未找到采用“Enemy”类型左侧操作数的运算符(或没有可接受的转换) - binary '==': no operator found which takes a left-hand operand of type 'Enemy' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM