【发布时间】:2023-03-10 06:43:02
【问题描述】:
我有一个错误提示:
error: no match for ‘operator>>’ (operand types are ‘std::ifstream {aka std::basic_ifstream<char>}’ and ‘const int*’)
inputStream >> getZipcode();
当我包含 #include <iostream> 和 #include <fstream> 并且我没有使用运算符重载函数时,所以我不明白这个问题。我还有一张纸条上写着:
In file included from /usr/include/c++/7/iostream:40:0,
from Strings.h:5,
from Sensor.h:5,
from Car.h:4,
from Agency.h:4,
from Agency.cpp:2:
/usr/include/c++/7/istream:168:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
operator>>(bool& __n)
^~~~~~~~
/usr/include/c++/7/istream:168:7: note: conversion of argument 1 would be ill-formed:
Agency.cpp:79:29: error: cannot bind non-const lvalue reference of type ‘bool&’ to an rvalue of type ‘bool’
inputStream >> getZipcode();
该函数应该为租车机构提供什么数据,包括该机构的名称和邮政编码。然后读入“库存”或代理商拥有的汽车数据,例如:品牌、型号、所有者、年份、传感器和基本价格。
这是我的 Agency 头文件和我在单独的源文件中对 getZipcode() 的实现:
class Agency {
private:
char m_name[256];
int m_zipcode[5];
Car m_inventory[5];
public:
Agency();
//~Agency();
const char *getName();
const int *getZipcode();
void setName(const char *name);
void setZipcode(const int *zipcode);
const Agency &operator[](const int index);
void readData();
void printData();
void printAvailableCars();
};
const int *Agency::getZipcode() {
return m_zipcode;
}
void Agency::readData() {
ifstream inputStream;
char inputfile[50];
const int *tempZip = getZipcode();
Car *tempInvt = m_inventory;
char tempMake[256], tempModel[256], tempOwner[256], tempType[256];
int tempYear;
float tempBaseprice;
bool tempAvailable;
Sensor tempSensor[3];
Sensor *sensorPtr = tempSensor;
cout << "Enter file name: ";
cin >> inputfile;
inputStream.open(inputfile);
if (inputStream.is_open()) {
inputStream >> m_name;
for (int i = 0; i < 5; i++) {
inputStream >> getZipcode();
tempZip++;
}
for (int j = 0; j < 5; j++) {
inputStream >> tempYear;
inputStream >> tempMake;
inputStream >> tempModel;
inputStream >> tempBaseprice;
tempInvt->setYear(tempYear);
tempInvt->setMake(tempMake);
tempInvt->setModel(tempModel);
tempInvt->setBaseprice(tempBaseprice);
inputStream.get();
while (inputStream.peek() != '}') {
inputStream >> tempType;
sensorPtr->setType(tempType);
if (myStringCompare(tempType, "gps") == 0) {
sensorPtr->setExtracost(5.0);
sensorPtr->getGps_cnt() + 1;
}
else if (myStringCompare(tempType, "camera") == 0) {
sensorPtr->setExtracost(10.0);
sensorPtr->getCamera_cnt() + 1;
}
else if (myStringCompare(tempType, "lidar") == 0) {
sensorPtr->setExtracost(15.0);
sensorPtr->getLidar_cnt() + 1;
}
else if (myStringCompare(tempType, "radar") == 0) {
sensorPtr->setExtracost(20.0);
sensorPtr->getRadar_cnt() + 1;
}
else {
sensorPtr->setExtracost(0.0);
}
sensorPtr++;
}
inputStream.get();
tempInvt->updatePrice();
inputStream >> tempAvailable;
inputStream >> tempOwner;
tempInvt->setAvailable(tempAvailable);
tempInvt->setOwner(tempOwner);
}
tempInvt++;
}
}
[1]: https://i.stack.imgur.com/G2XNv.png
【问题讨论】:
-
请提供minimal reproducible example 和完整的错误信息
-
您无法从 istream 中读取指针。
inputStream >> getZipcode();应该做什么? -
@idclev463035818 我添加了更多信息
-
你为什么不使用
std::string作为字符串? -
其中一部分是要了解您不需要字符串或数组的指针,因为您可以使用
std::string和std::vector/std::array。原始指针在现代 C++ 中很少见。编写一个交换函数,例如,对于指针来说是一个有效的用例,但即使这样,你也宁愿使用引用,甚至更好地使用已经存在的东西 (std::swap)
标签: c++ error-handling operator-overloading fstream iostream