gpt4 book ai didi

c++ - 无法通过函数传递 vector 中的 ifstream 指针

转载 作者:行者123 更新时间:2023-11-30 17:28:03 32 4
gpt4 key购买 nike

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

这一切都很好,至少当我刚刚通过函数传递单个 ifstream 对象时是这样。现在我有一个指向 ifstream 对象的指针 vector ,理论上指向我拥有的每个 house 对象,因为你不能简单地将 ifstream 对象传递到 vector 中。

我在尝试通过函数传递这些指针时不断遇到错误。

尽管我完全理解通过引用传递,但我很难理解指针。

#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   In function 'void place_object(int, int, std::ifstream*&, std::vector<std::vector<char> >&)':
111 8 C:\Users\r_bur_000\Documents\#PROGRAMS\Map_Generator\main.cpp [Error] invalid operands of types 'std::ifstream* {aka std::basic_ifstream<char>*}' and 'int' to binary 'operator>>'
112 5 C:\Users\r_bur_000\Documents\#PROGRAMS\Map_Generator\main.cpp [Error] request for member 'ignore' in 'is', which is of pointer type 'std::ifstream* {aka std::basic_ifstream<char>*}' (maybe you meant to use '->' ?)
117 15 C:\Users\r_bur_000\Documents\#PROGRAMS\Map_Generator\main.cpp [Error] no matching function for call to 'getline(std::ifstream*&, std::string&)'
117 15 C:\Users\r_bur_000\Documents\#PROGRAMS\Map_Generator\main.cpp [Note] candidates are:
53 0 c:\program files (x86)\dev-cpp\mingw32\lib\gcc\mingw32\4.8.1\include\c++\string In file included from 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++\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 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
1068 5 c:\program files (x86)\dev-cpp\mingw32\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.tcc [Note] template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
1068 5 c:\program files (x86)\dev-cpp\mingw32\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.tcc [Note] template argument deduction/substitution failed:
117 15 C:\Users\r_bur_000\Documents\#PROGRAMS\Map_Generator\main.cpp [Note] mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::ifstream* {aka std::basic_ifstream<char>*}'
52 0 c:\program files (x86)\dev-cpp\mingw32\lib\gcc\mingw32\4.8.1\include\c++\string In file included from 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++\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 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<class _CharT, class _Traits, class _Alloc> 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 [Note] template argument deduction/substitution failed:
117 15 C:\Users\r_bur_000\Documents\#PROGRAMS\Map_Generator\main.cpp [Note] mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::ifstream* {aka std::basic_ifstream<char>*}'
28 C:\Users\r_bur_000\Documents\#PROGRAMS\Map_Generator\Makefile.win recipe for target 'main.o' failed

最佳答案

您的问题在这里:

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

在第 //1 行上,您在此 while 循环 block 内在堆栈上声明一个 ifstream 对象。这意味着 while 循环结束后,该对象不再有效。

在第 //2 行上,您将获取该对象的地址,并将其放入 vector 中 - 希望以后该地址将有效。然而,正如我上面提到的,一旦离开 while 循环的范围,它就不是了。

此外,您永远不会为多个 ifstream 对象保留空间。您只是不断地重新初始化同一个对象,并将相同的(即将失效的)地址放入 vector 中。

你想做这样的事情:

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

使用new运算符在进程堆上为新的ifstream对象分配空间。该对象的地址存储在ifstr指针中。然后您可以将该对象的地址放入 vector 中。

每当您使用new时,请记住您必须最终通过伴随的delete释放分配的内存。

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

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com