2011-09-09 43 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

首先,你正在編寫一個指針,然後你沒有告訴C++如何的entry應以文字來表示。

爲它創建一個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

你需要'返回操作系統;' –

+0

@阿門:謝謝,修正了 – carlpett

0

由於entry是一個普通的結構,並且您尚未定義任何operator<<方法將其內容寫入輸出流,因此編譯器會在您嘗試直接寫出*pos時抱怨。

要麼你需要定義一個適當的operator<<方法,或通過一個寫出來的結構之一的成員,如

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

你應該重載operator <<entry

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

試一下荷蘭國際集團這樣的:

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的其他字段。通常,實際上,entryoperator<< 將輸出所有相關字段。另一種解決方案 是定義一個變壓器功能對象,並使用它:

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()); 

而且順便說一句,它通常被認爲preferrable通過由常量 參考矢量,而不是價值。