2016-08-30 7 views
0

楽しさと経験のために、Blobby Volley 2 1.0(Linux)のソースコードを変更しています。std :: ostreamはconst char配列を出力できませんか?

まあ... は、ソースコードを変更していますが、私はコンパイルするためのプログラムを手に入れることさえできません。 (?悲しいことではありません)

ここでエラーが発生するコードです:グラム++ 5.4.0でこれをコンパイルしようとすると

std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) { 
    return stream << val.name << " (" << val.hostname << ":" << val.port << ")"; 
    } 

は、以下の(単純化出力できます - 元の出力を

std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) { 
    stream << "hello"; //can't get simpler than this, right? 
    return stream; 
    } 
:私はこれまでのコードを簡素化

error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘const char [32]’)

return stream << val.name << " (" << val.hostname << ":" << val.port << ")";

:〜443行)エラーメッセージです

error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘const char [6]’)

stream << "hello";

それはこのようになります呼び出すコードだ:私が最も驚くべき見つける事は、我々はすべてstd::coutおよびその同類がchar配列を扱うことができることを知っているということです

std::cout << "duplicate server entry\n"; 
std::cout << info << "\n"; //it's called here 

を。例えば

、滞りなく動作します

#include <iostream> 
#include <string> 

int main() { 
    const char a[6] = "hello"; 
    std::cout << a << std::endl; //No problem here! 
    return 0; 
    } 


ああ、もう1つ。

私は<string>が含まれている場合、これは動作します:

std::ostream& operator<<(std::ostream& stream, const ServerInfo& val) { 
    stream << std::string("hello"); 
    return stream; 
    } 

誰もが私が欠けているものを知っていますか?


PS:私は「didnのなぜあなたは不思議に思っている場合:はここpastebin of the errors.

PPSです:

/* header include */ 
#include "NetworkMessage.h" 

/* includes */ 
#include <cstring> 

#include "UserConfig.h" 
#include "SpeedController.h" 

PPS:ここでは、要求されたヘッダですstd::ostreamが定義されていないというエラーが表示された場合は、3番目のパラグラップを確認してくださいサムの答えのh。

+0

ここに記載されているものに加えて、完全なエラーログを投稿できますか?そこに重要なことがあるかもしれません。 – templatetypedef

+0

@templatetypedef:あなたはそれをすべて肯定していますか? –

+1

通常は起こらないエラーを報告していますので、他にも何か問題があると思われます。あ、はい! :-) – templatetypedef

答えて

7

#include <iostream>が不足している可能性があるという事実は、シャーロック・ホームズのアプローチを使って、「不可能なものを取り除いたときに残っているものは何でも、真実でなければならない」と推測されました。

明らかに。std::ostreamは、const char *の過負荷を受け入れるのに問題はなかったはずです。

したがって、オーバーロード解決の苦情は、<iostream>が含まれていないことを意味する必要があります。ほとんどのC++ライブラリクラスは全面的に前方宣言されています。いくつかのランダムなヘッダファイルを含めると、無料ボーナスとして、std::ostreamの前方宣言を得る可能性があります。したがって、コンパイラは、このクラスが定義されていないと不平を言うことはありません。

ただし、<iostream>が含まれていないと、コンパイラはそこに定義されているすべてのオーバーロードを認識しません。それでおしまい。

+0

'#include '( 'const char *' overload - 27.7.3.1を宣言している) 'iostream'ではなく'ostream'を含む - 27.4.1)。 – vilpan

関連する問題