2014-10-03 55 views
0

我試圖製作一個將生成二維城市的程序。我從一個文件中獲取一個文件的寬度和長度(預定義),並將其傳遞給一個函數(place_object),該文件將它放在文本文件中。無法通過函數傳遞向量中的ifstream指針

這樣做很好,至少當我通過函數傳遞一個單獨的ifstream對象時,它確實如此。現在我有一個指向ifstream對象的指針矢量,理論上指向我擁有的每個房子對象,因爲您不能簡單地將ifstream對象傳遞給矢量。

我不斷收到錯誤,試圖通過函數傳遞這些指針。

我很難理解指針,雖然我理解通過引用完美罰款。

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <conio2.h> 
#include <constream> 

using namespace std; 

//...Structures... 
struct Data 
{ 
    int width; 
    int height; 
}; 

//...Global Variables... 

//...Function Prototypes... 
void set_data(Data&); 
void init_map(vector< vector<char> >&, Data&); 
void transfer_to_file(vector< vector<char> >&, ofstream&, Data&); 
void place_object(int,int,ifstream*&,vector< vector<char> >&); 

int main() 
{ 
    //...Path Address Files... 
    ofstream m("What_You_Need/map.txt"); 
    ifstream house_files("Data/File_Paths/houses.dat"); 
    //...Regular Variables... 
    Data d; 
    vector< vector<char> > map; 

    //...Structures... 
    vector<ifstream*> houses; 


    //...Load Vectors... 
    string path; 
    house_files >> path; 
    while(!house_files.eof()) 
    { 
     ifstream ifstr(path.c_str()); 
     houses.push_back(&ifstr); 
     house_files >> path; 
    } 

    //...Get Size... 
    do 
    { 
     cout << "What's the map size? (HxW): "; 
     cin >> d.width; 
     cin.ignore(10,'x'); 
     cin >> d.height; 
     if(cin.fail()) 
     { 
      cin.clear(); 
      cin.ignore(); 
     }  
    } 
    while((d.width < 300 || d.height < 300) || cin.fail()); 

    init_map(map,d); 

    //...TEST CODE HERE... 
    //...Structure Files... 
    getch(); 
    place_object(3,3,houses[0],map); 
    transfer_to_file(map,m,d); 

    //...File Close... 
    house_files.close(); 
    m.close(); 

    //...Misc... 
    return 0; 
} 

//...Funtions... 
void set_data(Data &d) 
{ 

} 

void init_map(vector< vector<char> > &map, Data &d) 
{ 
    for (int i = 0 ; i < d.height ; i++) 
    { 
     vector<char> row; // Create an empty row 
     for (int j = 0 ; j < d.width ; j++) 
     { 
      row.push_back('.'); // Add an element (column) to the row 
     } 
     map.push_back(row); // Add the row to the main vector 
    } 
} 

void transfer_to_file(vector< vector<char> > &map, ofstream &o, Data &d) 
{ 
    for(int i = 0 ; i < d.height ; i++) 
    { 
     for(int j = 0 ; j < d.width ; j++) 
      o << map[i][j]; 
     o << "\n"; 
    } 
} 

void place_object(int x, int y, ifstream *&is, vector< vector<char> > &map) 
{ 
    int width; 
    int height; 
    is >> width >> height; 
    is.ignore(100,'\n'); 

    for(int i = 0 ; i < height ; i++) 
    { 
     string s; 
     getline(is,s); 
     for(int j = 0 ; j < width ; j++) 
     { 
      map[i+y-1][j+x-1] = s[j]; 
     } 
     cout << endl; 
    } 
} 

錯誤(與使用 「新」 新運營商代碼): C:\用戶\ r_bur_000 \文件#PROGRAMS \ Map_Generator \ main.cpp中在函數「void place_object(INT,INT,標準: :ifstream * &,std :: vector> &)': 111 8 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp [錯誤]類型'std :: ifstream * {aka std: :'basic_ifstream }'和'int'to binary'operator >>' 112 5 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp [錯誤]請求成員'ignore'in'is',是指針類型'std :: ifstream {aka std :: basic_ifstream }'(也許你打算使用' - >'?) 117 15 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp [錯誤]沒有匹配函數調用'getline(std :: ifstream &,std :: string &)' 117 15 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp [備註]備註: 53 0 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ string從c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ string 40 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ locale_classes.h from c:\ program files(x86)\ dev- cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ locale_classes.h 41 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \在clude \ C++ \ bits \ ios_base.h from c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ ios_base.h 42 c:\ program文件(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ ios from c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8。 1 \ include \ C++ \ ios 38 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ ostream from c:\ program files(x86)\ dev -cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ ostream 39 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ iostream從C:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ iostream 1 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main。 cpp from main.cpp 1068 5 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ basic_string.tcc [注意] tem板的std :: basic_istream < _CharT,_Traits> &的std ::函數getline(標準:: basic_istream < _CharT,_Traits> &,性病:: basic_string的< _CharT,_Traits,_Alloc> &,_CharT) 1068 5 C:\程序文件(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ basic_string.tcc [注意]模板參數推導/替換失敗: 117 15 C:\ Users \ r_bur_000 \ Documents #PROGRAMS \ Map_Generator \爲主。cpp [注]不匹配的類型'std :: basic_istream < _CharT,_Traits>'和'std :: ifstream * {aka std :: basic_ifstream }' 52 0 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ string從c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ string 40 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ locale_classes.h from c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ locale_classes.h 41 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ ios_base.h 42 c:\ program files( x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ ios from c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ ios 38 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ ostream from c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ ostream 39 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ iostream from c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ iostream 1 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp from main.cpp 2793 5 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ basic_string .h [Note] template std :: basic_istream < _CharT,_Traits> & std :: getline(std :: basic_istream < _CharT,_Traits> &,std :: basic_string < _CharT,_Traits,_Alloc> &) 2793 5 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ C++ \ bits \ basic_string.h [注意]模板參數扣除/替換失敗: 117 15 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp [注意]不匹配的類型'std :: basic_istream < _CharT,_Traits>'和'std :: ifstream {aka std :: basic_ifstream *}' 28 C:\用戶\ r_bur_000 \文件#程序\ Map_Generator \ Makefile.win配方目標 'main.o' 失敗

+0

發佈錯誤,無論哪種方式''place_object'你不是解除引用是。 – user657267 2014-10-03 00:07:30

+0

我添加了錯誤。但是,如何「解除引用」? – Twonty2 2014-10-03 01:21:51

+0

您可以通過在指針之前放置一個'*',例如'* is >> width >> height;',或者使用' - >'來調用函數,例如'is-> ignore(100, '\ n');'你可能應該傳遞你的流作爲參考,然而,從函數聲明中移除指針並取消引用參數,而不是'place_object(3,3,* houses [0],map);' – user657267 2014-10-03 01:23:58

回答

0

你的問題是在這裏:

while(!house_files.eof()) 
{ 
    ifstream ifstr(path.c_str());  // 1 
    houses.push_back(&ifstr);   // 2 
    house_files >> path; 
} 

在線// 1,您在此while循環塊內部的堆棧上聲明ifstream對象。這意味着while循環結束後,該對象不再有效。

在線// 2,你將該對象的地址,並將其放入你的矢量 - 與希望在下面,該地址將是有效的。然而,正如我上面提到的,一旦你離開while循環的範圍,它不是。

此外,你永遠不會保留空間多個ifstream對象。你只是不斷重新初始化相同的對象,並將相同的(即將失效的)地址放入向量中。

你想要做這樣的事情:

while(!house_files.eof()) 
{ 
    ifstream *ifstr = new ifstream(path.c_str()); // 1 
    houses.push_back(ifstr);      // 2 
    house_files >> path; 
} 

使用new運營商分配對過程的堆空間用於新ifstream對象。該對象的地址存儲在ifstr指針中。然後,您可以將該對象的地址放入向量中。

無論何時您使用new,請記住您必須必須最終釋放分配的內存與隨附delete

+0

太棒了!這很有道理。但是,當我在這裏調用函數時,我仍然遇到錯誤,「place_object(3,3,houses [0],map);」。我應該改變它,「place_object(3,3,*房屋[0],地圖);」或者其他的東西? – Twonty2 2014-10-03 01:15:13