我想做一个程序,可以生成一个二维城市。我从一个文件中获取一个建筑物的宽度和长度(预定义),然后通过一个函数(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 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
你是在宣布一个 ifstream
对象 叠加 内的while循环块。这意味着在 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
.