2017-01-27 5 views
1

私はテキストファイルに書き込むこの機能を持っていますが、ofstreamを使用して出力する構文とは何か関係があります。 誰かが私のためにこれを診断するのに役立つことができますか?ofStreamエラー:テキストファイルに書き込みますか?

おかげで、/home/ubuntu/workspace/saveGame/sgFunc.cpp

を実行

エビン

int writeSave(string chName, string chSex, string chRace, 
       vector<int> chAttributes, int chLevel, int chStage) 
{ 
    ofstream outputFile("saveFile.txt"); 
    outputFile << "chName: " << chName << 
        "\nchSex: " << chSex << 
        "\nchRace: " << chRace << 
        "\nchAttributes: " << chAttributes << 
        "\nchLevel: " << chLevel << 
        "\nchStage: " << chStage; 
    return 0; 
} 

/home/ubuntu/workspace/saveGame/sgFunc.cpp: In function ‘int writeSave(std::string, std::string, std::string, std::vector<int>, int, int)’: /home/ubuntu/workspace/saveGame/sgFunc.cpp:27:44: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’ 
        "\nchRace: " << chRace << 
              ^

In file included from /usr/include/c++/4.8/iostream:39:0, 
       from /home/ubuntu/workspace/saveGame/sgFunc.cpp:1: /usr/include/c++/4.8/ostream:602:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::vector<int>]’ 
    operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x) 
    ^
+0

''ベクトル 'の '' <<'演算子を再定義しない限り、単純に '<< chAttributes'を実行することはできません –

答えて

5

std::vector<>はに(デフォルトで)ストリーミングすることができませんあなたがしようとしている文字出力ストリームは<< chAttributesです。手動で文字列に変換するか、文字出力ストリームに文字列をストリーミングするためにoperator<<を指定する必要があります。

あなたはカンマで区切られた内容を書きたい場合は、1つのオプション、(あなたは<iterator><algorithm>を含める必要があります):あなたのコードがに表示される


outputFile << "chName: " << chName << 
       "\nchSex: " << chSex << 
       "\nchRace: " << chRace << 
       "\nchAttributes: "; 

copy(chAttributes.begin(), 
    chAttributes.end(), 
    ostream_iterator<int>(outputFile, ",")); 

outputFile << "\nchLevel: " << chLevel << 
       "\nchStage: " << chStage; 
私は using namespace std;を想定し、この例のコードを書きました。私はこの行を使用しないことをお勧めします。代わりに std名前空間から使用したいものを std:: -qualifyしてください。

関連する問題