『Modern C++ チャレンジ』問題15
Githubのサンプル
『Modern C++ チャレンジ』問題15の中で下のような処理があるのですが、
"sstr << *this;"箇所で次のエラーメッセージが出ます。
no operator "<<" matches these operands -- operand types are: std::stringstream << const ipv4
このエラーについて次の三点がわかりません。
- GCC(8.3.0)を使うとエラーが出るが、Visual Studio 2017 Comunity だと出ない。
(本書にもVisual Studioでコンパイルできることを確認したと記述がある。)
2. GCC(8.3.0)でエラーが出たままコンパイルができ、to_string()を使って文字変換もできる。
3. operator<<()の定義をto_string()の上に書いてもエラーは消えないが、宣言を書くと消える。
わかる方がいらっしゃいましたら、ご教示ください。
よろしくお願いいたします。
C++
1 std::string to_string() const 2 { 3 std::stringstream sstr; 4 sstr << *this; 5 return sstr.str(); 6 } 7 8//~省略~ 9 10 friend std::ostream& operator<<(std::ostream& os, const ipv4& a) 11 { 12 os << static_cast<int>(a.data[0]) << '.' 13 << static_cast<int>(a.data[1]) << '.' 14 << static_cast<int>(a.data[2]) << '.' 15 << static_cast<int>(a.data[3]); 16 return os; 17 }