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

Unable to overload stream extraction operator (>>) in C++

本文关键字:gt 运算符 提取 C++      更新时间:2023-10-16

我目前正在尝试理解C++运算符重载的基本概念。因此,我创建了一个类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

轮子.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;
}

主.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

我在这里错过了什么?

您需要像这样#include <iostream>包含iostream的输入和输出,这样可以避免错误。

输出时,它是定义<<运算符的类,您总是使用 ostream,但您忘记了输入>> 的情况。


#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 */

正如@Ext3h指出的那样,我缺少istream的相应包含。

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

非常感谢您的帮助,我会花很长时间来解决这个问题!