質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

2回答

423閲覧

operator<< について

miyatoshi24

総合スコア17

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

1グッド

1クリップ

投稿2019/03/04 12:55

『Modern C++ チャレンジ』問題15
Githubのサンプル

『Modern C++ チャレンジ』問題15の中で下のような処理があるのですが、
"sstr << *this;"箇所で次のエラーメッセージが出ます。

no operator "<<" matches these operands -- operand types are: std::stringstream << const ipv4

このエラーについて次の三点がわかりません。

  1. 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 }
hon.ki👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cateye

2019/03/04 13:16 編集

エラーメッセージは1行だけですか?
miyatoshi24

2019/03/04 21:02

はい。"sstr << *this; の部分に赤波線がでて、 no operator "<<" matches these operands -- operand types are: std::stringstream << const ipv4 が表示されます。
guest

回答2

0

こんにちは。

少し補って、gcc 8.3.0とclang 9.0.0でやってみましたが、エラーも警告もでませんでした。
省略された部分に原因が潜んでいそうです。

C++

1#include <iostream> 2#include <sstream> 3 4struct ipv4 5{ 6 std::string to_string() const 7 { 8 std::stringstream sstr; 9 sstr << *this; 10 return sstr.str(); 11 } 12 13//~省略~ 14 15 friend std::ostream& operator<<(std::ostream& os, const ipv4& a) 16 { 17 os << static_cast<int>(a.data[0]) << '.' 18 << static_cast<int>(a.data[1]) << '.' 19 << static_cast<int>(a.data[2]) << '.' 20 << static_cast<int>(a.data[3]); 21 return os; 22 } 23 24 int data[4]; 25 ipv4() : data{1, 2, 3, 4} { } 26}; 27 28int main() 29{ 30 ipv4 foo; 31 std::cout << foo.to_string() << std::endl; 32}

wandbox

投稿2019/03/04 13:50

Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

miyatoshi24

2019/03/04 20:58

回答ありがとうございます。 ソースコードは「Githubのサンプル」にあるのとほとんど変わらないので、 省略部分とは関係ないと思っています。 VSCodeとGCC/G++(8.3.0)を使っているのですが、確認してくださったソースコードでも 「問題」として同じメッセージが出てきたので、自分のコンパイル環境の問題かもしれないですね。
miyatoshi24

2019/03/04 21:08 編集

c_cpp_properties.jsonには以下のように設定しています。 { "name": "Win32", "compilerPath": "C:/msys64/mingw64/bin/g++.exe", "includePath": [ "${workspaceFolder}", "C:/msys64/mingw64/include/c++/8.3.0", "C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/include" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "intelliSenseMode": "gcc-x64", "browse": { "path": [ "${workspaceFolder}", "C:/msys64/mingw64/include/c++/8.3.0", "C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/include" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" }, "cStandard": "c11", "cppStandard": "c++17" }
Chironian

2019/03/05 04:05

少なくとも古いVisual Studioで表示される赤波線は無視していました。エラーでないのにエラー表示されることがあり、かなり追求したことがあるのですが解決しませんでした。(他にも同様な問題でハマった方が複数見つかりましたし。) VSCodeは使っていないので、同様な問題があるのかどうかは把握していませんが、コンパイル・エラーや警告が出ないのであれば赤波線は無視しても問題ないだろうと思います。
Chironian

2019/03/05 04:09

あ、因みに、linux上のgccとWindows上のMinGWは同じものではないです。 そこそこ相違があるので環境の記述時は区別されることをお勧めします。
miyatoshi24

2019/03/10 04:54 編集

ためしに、mingw-get-setupを使用してみたところ、上記のメッセージは消えました。 あらたに「--exec-charset=cp932」が使えないという問題がでてきましたが、 ググって調べようと思います。 ありがとうございました。
guest

0

そのままコンパイルしてみましたが・・・

text

1usr ~/Project/test % c++ t.cpp 2t.cpp:57:19: warning: implicit conversion loses integer precision: 'int' to 'unsigned char' 3 [-Wimplicit-int-conversion] 4 a = ipv4(b1, b2, b3, b4); 5 ~~~~ ^~ 6t.cpp:57:23: warning: implicit conversion loses integer precision: 'int' to 'unsigned char' 7 [-Wimplicit-int-conversion] 8 a = ipv4(b1, b2, b3, b4); 9 ~~~~ ^~ 10t.cpp:57:27: warning: implicit conversion loses integer precision: 'int' to 'unsigned char' 11 [-Wimplicit-int-conversion] 12 a = ipv4(b1, b2, b3, b4); 13 ~~~~ ^~ 14t.cpp:57:31: warning: implicit conversion loses integer precision: 'int' to 'unsigned char' 15 [-Wimplicit-int-conversion] 16 a = ipv4(b1, b2, b3, b4); 17 ~~~~ ^~ 184 warnings generated. 19usr ~/Project/test % g++ t.cpp 20usr ~/Project/test %

clang++はワーニングですね?
clang version 9.0.0 (trunk 354955)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /xxxxx/bin

clang++ -Wno-c++98-compat -Wno-padded -pipe -std=c++17 -Weverything -Ofast

g++はワーニングも出ない
g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE

g++ -pipe -std=c++14 -Wall -Ofast

投稿2019/03/04 13:29

編集2019/03/04 13:31
cateye

総合スコア6851

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cateye

2019/03/04 13:32

回答になっていない^^:
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問