无法在C ++中使流提取运算符(>>)过载

时间:2017-12-02 11:29:24

标签: c++ gcc c++17

我目前正在尝试理解C ++中运算符重载的基本概念。因此,我创建了一个类wheel,它可以通过重载流提取运算符>>来读取流。

wheel.h

#ifndef WHEEL_H
#define WHEEL_H

#include <cassert>
#include <ostream>
#include <string>

class wheel final {
    float rimDiameter;
    int productionYear;
    std::string velocityIndex = "N/A";
    std::string manufacturer = "N/A";

public:
    wheel() = default;

    wheel(  float rimDiameter,
            int productionYear,
            std::string velocityIndex,
            std::string manufacturer
        ) : rimDiameter{rimDiameter},
            productionYear{productionYear},
            velocityIndex{velocityIndex},
            manufacturer{manufacturer}
        {}

    ~wheel() = default;

    friend
    auto operator<<(std::ostream &os, const wheel &self) -> std::ostream &;

    friend
    auto operator>>(std::istream &is, wheel &self) -> std::istream &;
};

#endif

wheel.cpp

#include "wheel.h"

auto operator<<(std::ostream &os, const wheel &self) -> std::ostream & {
    return os << "   WHEEL" << std::endl
        << "   =============================" << std::endl
        << "      Rim Diameter:       " << self.rimDiameter << "\"" << std::endl
        << "      Year of Production: " << self.productionYear << std::endl
        << "      Velocity Index:     " << self.velocityIndex << std::endl
        << "      Manufacutrer:       " << self.manufacturer << std::endl;
}


auto operator>>(std::istream &is, wheel &self) -> std::istream & {
    char c[3];

    is >> 
        self.rimDiameter >> c[0] >> 
        self.productionYear >> c[1] >>
        self.velocityIndex >> c[2] >> 
        self.manufacturer;

    assert(c[0] == ';' && c[1] == ';' && c[2] == ';');
    return is;
}

的main.cpp

#include <iostream>
#include "wheel.h"

int main(void) {
    wheel w;

    std::cin >> w;
    std::cout << w;

    return 0;
}

对我而言,一切看起来都不错,但不知怎的,我不断收到错误,告诉我有no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'float')

$ make
g++-7 -g -std=c++17 -Wall -Wextra -Wconversion -Wpedantic -c main.cpp
g++-7 -g -std=c++17 -Wall -Wextra -Wconversion -Wpedantic -c wheel.cpp
wheel.cpp: In function 'std::istream& operator>>(std::istream&, wheel&)':
wheel.cpp:16:8: error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'float')
     is >>
     ~~~^~
         self.rimDiameter >> c[0] >>
         ~~~~~~~~~~~~~~~~
wheel.cpp:13:6: note: candidate: std::istream& operator>>(std::istream&, wheel&)
 auto operator>>(std::istream &is, wheel &self) -> std::istream & {
      ^~~~~~~~
wheel.cpp:13:6: note:   no known conversion for argument 2 from 'float' to 'wheel&'
In file included from /usr/local/Cellar/gcc/7.2.0/include/c++/7.2.0/string:53:0,
                 from /usr/local/Cellar/gcc/7.2.0/include/c++/7.2.0/bits/locale_classes.h:40,
                 from /usr/local/Cellar/gcc/7.2.0/include/c++/7.2.0/bits/ios_base.h:41,
                 from /usr/local/Cellar/gcc/7.2.0/include/c++/7.2.0/ios:42,
                 from /usr/local/Cellar/gcc/7.2.0/include/c++/7.2.0/ostream:38,
                 from wheel.h:5,
                 from wheel.cpp:1:
/usr/local/Cellar/gcc/7.2.0/include/c++/7.2.0/bits/basic_string.tcc:1465:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator>>(basic_istream<_CharT, _Traits>& __in,
     ^~~~~~~~
/usr/local/Cellar/gcc/7.2.0/include/c++/7.2.0/bits/basic_string.tcc:1465:5: note:   template argument deduction/substitution failed:
wheel.cpp:17:14: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'float'
         self.rimDiameter >> c[0] >>
              ^~~~~~~~~~~
make: *** [wheel.o] Error 1

我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

您需要包含iostream作为输入和输出,例如#include <iostream>,您就可以避免错误。

输出时,&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;运算符定义为您总是使用ostream,但是您忘记了输入&gt;&gt;的情况。

#ifndef WHEEL_H
#define WHEEL_H

#include <cassert>
#include <iostream>
#include <string>

class wheel final {
    float rimDiameter;
    int productionYear;
    std::string velocityIndex = "N/A";
    std::string manufacturer = "N/A";

public:
    wheel() = default;

    wheel(  float rimDiameter,
          int productionYear,
          std::string velocityIndex,
          std::string manufacturer
          ) : rimDiameter{rimDiameter},
    productionYear{productionYear},
    velocityIndex{velocityIndex},
    manufacturer{manufacturer}
    {}

    ~wheel() = default;

    friend
    auto operator<<(std::ostream &os, const wheel &self) -> std::ostream &;

    friend
    auto operator >> (std::istream &is, wheel &self) -> std::istream &;
};

#endif /* wheel_hpp */

答案 1 :(得分:0)

正如@ Ext3h指出的那样,我错过了istream的相应包含。

#include <istream>添加到wheel.h可以解决问题。

非常感谢你的帮助,我会永远把我弄清楚这个问题!