[英]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-
接受int
和string
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.