2012-09-02 6 views
6

私は私の方法のいずれかからの出力に戻り値をしようとすると、私はエラーを取得しています:なぜstd :: stringを書き込んで、未知の演算子<<エラー?

Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string 

MAIN.CPP

#include <iostream> 
using namespace std; 

#include "Book.h" 

int main() 
{ 
    book.setTitle("Advanced C++ Programming"); 
    book.setAuthorName("Linda", "Smith"); 
    book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond"); 
    book.setPrice(49.99); 

    cout << book.getBookInfo(); // <-= this won't compile because of the error above. 

    int i; 
    cin >> i; 

    return 0; 
}; 

文字列を返す必要があります方法:

string Book::getBookInfo() 
{ 
    stringstream ss; 
    ss << title << endl << convertDoubleToString(price) << endl; 

    return ss.str(); 
} 
+0

あなたは '<<演算子」をオーバーロードする必要があります。 – Rapptz

+2

@Rapptz - いいえ、ここではありません。メンバ関数 'getBookInfo'は文字列を返し、その文字列が挿入されます。 –

+2

'str :: string'とは何ですか? –

答えて

24

#include <string>がありません。

+1

問題が解決しました...私は今、愚かな気分です。私はそこにそれを持っていませんでした。ありがとう、私は次回のために覚えています。 – HelpNeeder

2

コードはどのようにしてstringの定義を取得しましたか?ヘッダ<string>は、ストリームインサータも宣言します。

関連する問題