【问题标题】:error: no match for ‘operator>>’ for my class错误:我的班级的“操作员>>”不匹配
【发布时间】: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 &lt;iostream&gt;#include &lt;fstream&gt; 并且我没有使用运算符重载函数时,所以我不明白这个问题。我还有一张纸条上写着:

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 &gt;&gt; getZipcode(); 应该做什么?
  • @idclev463035818 我添加了更多信息
  • 你为什么不使用std::string作为字符串?
  • 其中一部分是要了解您不需要字符串或数组的指针,因为您可以使用std::stringstd::vector / std::array。原始指针在现代 C++ 中很少见。编写一个交换函数,例如,对于指针来说是一个有效的用例,但即使这样,你也宁愿使用引用,甚至更好地使用已经存在的东西 (std::swap)

标签: c++ error-handling operator-overloading fstream iostream


【解决方案1】:

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

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

【讨论】:

  • 您不需要循环来读取 4 位数字。实际上,如果文件确实包含 4 位数字,那么 inputStream &gt;&gt; aux_Zip[0]; 将已经读取所有 4 位数字
  • 顺便说一句,我也不能同意“不建议使用 get 来更改类的内部数据”。当然,getter 会得到一些不设置的东西,但有时 getter 会做一些记账,然后他们会修改内部成员,我看不出有什么不好的
  • 或者考虑一个惰性求值的类私有成员,如果需要,相应的getter会首先检索值,将其存储在内部,然后才返回它
  • 好的,首先是循环,这完全取决于输入的格式,我假设格式是 4 4 4 4,所以正如我所说,信息很少。和get的事情有关,我所有的老师都说过,我们要保证类的数据成员的封装。
  • 如果问题不清楚,最好要求澄清,而不是根据猜测回答。其次,我的反驳论点与封装没有对比。相反,如果你在getter中修改了一个私有封装的成员,这个就是封装。
【解决方案2】:

getZipcode() 是对返回 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; 
}

【讨论】:

  • 在什么情况下从流中读取int* 有意义(假设它可以工作)?
  • 这是原始帖子中给定的数据类型(是的,它有效)。错误不是来自指针,而是他试图提取到const int* 的事实。这不会被覆盖,例如尝试提取到 const int 是行不通的。
  • 很难想象从文件中读取int* 是 OP 真正想要的。
  • 您好!我刚刚在我的问题中添加了更多信息。抱歉没有说清楚。
  • 为什么在你的 getter 和 setter 函数中需要const int* 来获取邮政编码?只需int就足够了。为什么你用这个inputStream &gt;&gt; getZipcode(); 来读取邮政编码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 2018-05-20
  • 2019-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多