我正在尝试制作一个可以生成二维城市的程序。我正在从文件中获取具有建筑物的宽度和长度(预定义)的文件,并通过将其放置在文本文件中的函数(place_object)传递。

一切正常,至少在我通过函数传递单个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;
    }
}


错误(使用使用“ new”运算符的新代码):
C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp在函数'void place_object(int,int,std :: ifstream *&,std :: vector>&)'中:
111 8 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp [错误]类型'std :: ifstream * {aka std :: basic_ifstream}'和'int'到二进制'operator >>'的无效操作数
112 5 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ main.cpp [错误]请求'is'中的成员'ignore',其指针类型为'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:\程序文件(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ string在c:\ program文件(x86)\ dev-cpp \ mingw32包含的文件中\ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ string
40个c:\ program files(x86)\ dev-cpp \ mingw32中的c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ bits \ locale_classes.h \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ bits \ locale_classes.h
41 c:\ program files(x86)\ dev-cpp \ mingw32中的c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ bits \ ios_base.h \ 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来自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来自main.cpp
1068 5 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ bits \ basic_string.tcc [注意]模板std :: basic_istream & std :: getline(std :: basic_istream &,std :: basic_string &,_CharT)
1068 5 c:\ program files(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 \ main.cpp [注意]类型'std :: basic_istream '和'std :: ifstream * {aka std :: basic_ifstream}'不匹配
52 0 c:\程序文件(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ string在c:\ program文件(x86)\ dev-cpp \ mingw32包含的文件中\ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ string
40个c:\ program files(x86)\ dev-cpp \ mingw32中的c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ bits \ locale_classes.h \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ bits \ locale_classes.h
41 c:\ program files(x86)\ dev-cpp \ mingw32中的c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ bits \ ios_base.h \ 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来自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来自main.cpp
2793 5 c:\ program files(x86)\ dev-cpp \ mingw32 \ lib \ gcc \ mingw32 \ 4.8.1 \ include \ c ++ \ bits \ basic_string.h [注意]模板std :: basic_istream & std :: getline(std :: basic_istream &,std :: basic_string &)
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 '和'std :: ifstream {aka std :: basic_ifstream *}'不匹配
目标'main.o'的28 C:\ Users \ r_bur_000 \ Documents#PROGRAMS \ Map_Generator \ Makefile.win配方失败

最佳答案

您的问题在这里:

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释放分配的内存。

关于c++ - 无法通过函数在 vector 中传递ifstream指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26171475/

10-09 08:56