error: no match for ‘operator>>’ for my class

error: no match for ‘operator>>’ for my class

我有一条错误消息:

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();

此函数应该为租车代理机构提供什么数据,包括代理机构的名称和邮政编码。然后读取 "inventory" 或代理商拥有的汽车数据,例如:制造商、型号、所有者、年份、传感器和基本价格。

这是我的 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

该错误来自于尝试使用带指针的istream,还有一个设计错误,不建议使用get来更改class的内部数据,所以你最好编程一个集合函数。我假设您正在尝试按数字及其 4 位数字读取 ZipCode 号码。但是,如果您不想这样做,请给我更多信息和代码。

int aux_Zip[4];
for (int i = 0; i < 5; i++) {
    inputStream >> aux_Zip[i];
}
setZipCode(aux_Zip);

getZipcode() 是对 returns 和 const int* 的函数的调用。从您的 inputStream 读取整数(邮政编码)并尝试将其存储在函数调用中没有任何意义。

如果您想从文件中读取邮政编码,这会起作用:

int *tempZip = getZipcode();
if (inputStream.is_open()) {
    inputStream >> m_name;
    inputStream >> tempZip; 
}

如果您想从输入流中提取邮政编码 int *tempZip 不能是 const

如评论所述,更好的解决方案是直接读取 int 而无需使用 int*

int tempZip = getZipcode(); // not sure what getZipcode() should do
if (inputStream.is_open()) {
    inputStream >> m_name;
    inputStream >> tempZip; 
}