2017-12-02 20 views
-1

現在、C++の演算子オーバーロードの基本的な概念を理解しようとしています。したがって、ストリーム抽出演算子>>をオーバーロードしてストリームから読み取ることができるクラスwheelを作成しました。私に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 

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; 
} 

、それはすべてB、正常に見えます私がここで行方不明です何

$ 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 

:UTは何とか私はno match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'float')があることを私に言って、エラーを得続けますか?

+2

istreamはどこに含まれていますか? – Ext3h

+0

@ Ext3hどこにも、それが問題でした。そんなことを指摘してくれてありがとう:) – IggyBlob

答えて

0

Ext3hが指摘@として、私は、それぞれがistreamのために含まれないのです。

からwheel.hを追加すると問題が解決されます。

ご協力いただきありがとうございます。問題を解決するために永遠に私を連れて行きましたでしょうか。

0

このような入力と出力にiostreamを含める必要があります。#include <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 */ 
関連する問題