2011-09-09 14 views
0

ベクトルvの内容をファイルに書きたいと思います。問題は、コンテンツではなくアドレスがテキストファイル内に配置されることです。ベクトルデータをファイルに書き込むC++の問題

私はエラーを取得する& POSの*のPOSインプレース書く:エラーC2679:バイナリ「< <」:何のオペレータがどのよう

型「エントリー」の右辺のオペランドをとる見つからなかったがそれは正しい?

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <limits> 

/* 
Data.txt 

John 
6543 
23 

Max 
342 
2 

A Team 
5645 
23 
*/ 

struct entry 
{ 
    // passengers data 
    std::string name; 
    int weight; // kg 
    std::string group_code; 
}; 

void reservations(std::vector<entry> v) 
{ 
    std::ofstream outfile; 
    outfile.clear(); 
    outfile.open("reservations.txt"); 
    // print data in vector 
    std::vector<entry>::iterator pos; 
     for (pos = v.begin(); pos!= v.end();++pos) 
     { 
      outfile << &pos << std::endl; 
      std::cout << &pos << std::endl; 
     } 
    outfile.close(); 
} 

entry read_passenger(std::ifstream &stream_in) 
{ 
    entry passenger; 
    if (stream_in) 
    { 
     std::getline(stream_in, passenger.name); 
     stream_in >> passenger.weight; 
     stream_in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
     std::getline(stream_in, passenger.group_code); 
     stream_in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
     return passenger; 
    } 

    return passenger; 
} 

int main(void) 
{ 
    std::ifstream stream_in("data.txt"); 
    std::vector<entry> v; // contains the reservations 
    std::vector<entry> a; // contains the cancellations 
    const int limit_total_weight = 10000; // kg 
    int total_weight = 0;     // kg 
    entry current; 
    while (stream_in) 
    { 
     current = read_passenger(stream_in); 
     if (total_weight + current.weight >= limit_total_weight) 
     { 
      // push data (cancellations) to vector 
      a.push_back(current); 
     } 
     else 
     { 
      total_weight = total_weight + current.weight; 
      // push data (reservations) to vector 
      v.push_back(current); 
     } 
    } 
    reservations(v); // write reservations to file 
    std::cout << "Rest " << limit_total_weight - total_weight << "kg" <<std::endl; 
    return 0; 
} 

答えて

0

まずあなたがポインタを書いていたし、次にあなたがentryはテキストで表現する方法をC++を教えてくれませんでした。

オーバーロードのためにoperator<<を作成します。

+1

申し訳ありませんが、私はC++の初心者です!それはどういう意味ですか? – burner007

0

outfile << pos->name << " " << pos->weight << /* etc */を実行するか、出力演算子operator<<を実装する必要があります。

、必要に応じてそれは

std::ostream& operator<<(std::ostream& os, const entry&) { 
     return os << entry.name << " " << entry.weight << " " << entry.group_code; 
    } 

フォーマットのようになります。

+0

あなたは 'os;を返す必要があります –

+0

@Armen:ありがとう、固定 – carlpett

0

entryはプレーンな構造体で、operator<<メソッドを定義していないため、その内容を出力ストリームに書き込むことはできません。*posを直接書き出すときにコンパイラがエラーになります。

あなたは、適切なoperator<<方法を定義する、または1つによって構造体1のメンバーを書き出す、

 outfile << pos->name << std::endl; 
     outfile << pos->weight << std::endl; 
     outfile << pos->group_code << std::endl; 
4

のようにあなたがentryためoperator <<をオーバーロードする必要がありする必要が次のいずれか

std::ostream& operator << (std::ostream& o, const entry& e) 
{ 
    return o << e.name 
    << " " << e.weight 
    << " " << e.gruop_code; 
} 

書くことができます:

outfile << *pos << std::endl; 
std::cout << *pos << std::endl; 
+0

はい、それは、ありがとう! – burner007

0

以下のようなINGの:

std::ostream& operator<<(std::ostream& os,const entry& toPrint) 
{ 
    os << "name :" << toPrint.name << '\n'; 
    os << "weight :" << toPrint.weight << "(kg) \n"; 
    os << "group_code :" << toPrint.group_code << '\n'; 
    return os; 
} 

も、あなたがのアドレスを出力しているので、あなたの出力は、現在のコードでアドレスのように見える理由は ある

void reservations(const std::vector<entry>& v) 
0

に予約機能の署名を変更する場合がありますイテレータその右側のオペランドとしてentryを取る何<< オペレータが存在しないため

「型 『エントリー』の 右側の演算子を取る何オペレータが見つからない」エラーが出た理由はあります。 は簡単に定義できます。

std::ostream& 
operator<<(std::ostream& dest, entry const& obj) 
{ 
    dest << obj.name; 
    return dest; 
} 

これは少し特殊です。他の文脈では、 には、他のフィールドをentryとして出力したいかもしれません。古典的には、実際にはoperator<<entry)はすべての関連フィールドを出力します。

struct EntryToName 
{ 
    std::string operator()(entry const& obj) const 
    { 
     return obj.name; 
    } 
}; 

そしてreservationsで:別の溶液 トランス機能オブジェクトを定義し、それを使用することであろう

std::transform( 
    v.begin(), v.end(), 
    std::ostream_iterator<std::string>(outfile, "\n"), 
    EntryToName()); 

そしてところで、一般にCONST 参照によってベクトルを渡すpreferrable考えです価値よりもむしろ。

関連する問題