Can't getline from ifstream?

Me and my friend decided to make a game, I'm the programmer and he's the artist...
You can't say I am new to C++ because I program like for 4 years now, But I never done something really serious like this little project I mentioned above.

So I stumbled upon something that really confuses me...
I use Microsoft's Visual Studio 2010 and I have two projects I use - A test console to test random code bits while I don't have the game set up yet. and the game project itself...
so in my test console the following code worked fine:
1
2
3
4
5
6
	string buffstring, workstring;							//Declarations

	ifstream lvlfile("testlevel.lvl");						//Load file.
	if(!lvlfile.is_open())					return(false);		//Check if opened.
	if(!lvlfile.good())					return(false);		//Check if useable.
	getline(lvlfile,buffstring,'#');						//Get a line from the file. 

but when I put it into my game project it just won't recognize the file, it will read it as "" also when I declare ifstream lvlfile; in the class's private section and then do lvlfile.open("testlevel.lvl"); the compiler gives me an
Error: no instance of overloaded function "getline" matches the argument list"
on all "getline"s in the source-code.

Any one can explain me why that happens, and why exactly the same code work in one project and doesn't in the other?
Could it be because I used classes in my game and didn't on my test project?'
If you need me to post the source just tell me to, its 3 files:
main.cpp
mapreader.cpp
mapreader.h
It's just a lot of code and I didn't want to flood the topic with it :)
closed account (DSLq5Di1)
Have you included all the appropriate headers in your game project/class? fstream? string?.. other than that I'm not sure, posting the code would save us some guessing.
Sorry for not answering for a few days, I bought a laptop and gave my father my previous PC.
Now I am at it, and can answer... so umm yea, I included everything. here is the code:
"mapreader.h":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class tile		//#CLASS# Tile class :)
{
private:
	SDL_Rect box;
	char* type;

public:
	void set(int x, int y, char* tileType, int w = 32, int h = 32);
	//void show();
	//int get_type();
	//SDL_Rect get_box();
	//tile();
	//~tile();
};

class map		//#CLASS# Map class
{
private:
	short unsigned int spritecount;
	ifstream lvlfile;

public:
	bool load(const char* mapname);
	tile* sprites;
	~map();//{ if(sprites != NULL) delete [] sprites;}
};

"mapreader.cpp":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "main.h"
#include "mapreader.h"
#include <string>
#include <fstream>
using namespace std;

void tile::set(int x, int y, char* tileType, int w, int h)
{
	box.x = x;
	box.y = y;
	box.w = w;
	box.h = h;
	type = tileType;
}

bool map::load(const char* mapname)
{
	string buffstring, workstring;							//Declarations

	//ifstream lvlfile("testlevel.lvl");						//Load file.
	lvlfile.open("testlevel.lvl");
	if(!lvlfile.is_open())				return(false);		//Check if opened.
	if(!lvlfile.good())					return(false);		//Check if useable.
	getline(lvlfile,buffstring,'#');						//Get a line from the file.
	if(buffstring != "blocklist") {lvlfile.close(); return(false);}		//Check if file valid.
	getline(lvlfile,buffstring,'#');
	if(!(bool)buffstring.find("spritecount")) {lvlfile.close(); return(false);}
	if(buffstring.find_first_of(",") == buffstring.npos) {lvlfile.close(); return(false);}
	workstring = buffstring.substr((buffstring.find_first_of(",") + 1));
	spritecount = atoi(workstring.c_str());					//Sets the sprite count! :D
	sprites = new tile[spritecount];
	for(short i = 0; i < spritecount; i++)
	{
		int x,y,w,h;
		getline(lvlfile,buffstring,'#');							//Set data line to buffer
		size_t f_note = buffstring.find_first_of(",") - 1;			//f_note = first acc of ","
		workstring = buffstring.substr(0,f_note);					//copy str from 0 to 1st,
		char* tileType = (char*)workstring.c_str();					//set tileType to str
		//## Basically looped:
		size_t s_note = buffstring.find_first_of(",",f_note);		//s_note = 2nd, //n_note = 1st,
		workstring = buffstring.substr(f_note,s_note);				//copy str from 1st, to 2nd,
		x = atoi(workstring.c_str());								//set x to str.
		f_note = s_note;											//make 2nd, count as 1st,
		//## Second time:
		s_note = buffstring.find_first_of(",",f_note);				//s_note = 3nd, //n_note = 2st,
		workstring = buffstring.substr(f_note,s_note);				//copy str from 2st, to 3nd,
		y = atoi(workstring.c_str());								//set x to str.
		f_note = s_note;											//make 3nd, count as 2st,
		//## Third time:
		s_note = buffstring.find_first_of(",",f_note);				//s_note = 4nd, //n_note = 3st,
		workstring = buffstring.substr(f_note,s_note);				//copy str from 3st, to 4nd,
		w = atoi(workstring.c_str());								//set x to str.
		f_note = s_note;											//make 4nd, count as 3st,
		//## Fourth time:
		s_note = buffstring.find_first_of(",",f_note);				//s_note = 5nd, //n_note = 4st,
		workstring = buffstring.substr(f_note,s_note);				//copy str from 4st, to 5nd,
		h = atoi(workstring.c_str());								//set x to str.
		f_note = s_note;											//make 5nd, count as 4st,
		//## End of loop!
		sprites[i].set(x,y,tileType,w,h);
	}
	return(true);
}

"main.h":
1
2
3
4
5
6
7
8
9
#include "main.h"
#include "mapreader.h"

int main(int argc, char* args[])
{
	map map;
	map.load("testlevel.lvl");
	return 0;
}


Also you might notice an include of "main.h", it has only one line: #include "SDL.h"

[u]EDIT:[/u]
Also here is the simple content of testlevel.lvl:
blocklist#
spritecount,16#
dirt_tr_00,0,0,32,32#
dirt_tt_00,32,0,32,32#
dirt_tl_00,64,0,32,32#
dirt_ll_00,0,32,32,32#
dirt_cc_00,32,32,32,32#
dirt_rr_00,64,32,32,32#
dirt_bl_00,0,64,32,32#
dirt_bb_00,32,64,32,32#
dirt_br_00,64,64,32,32#
dirt_narrow_vt_00,96,0,32,32#
dirt_narrow_vc_00,96,32,32,32#
dirt_narrow_vb_00,96,64,32,32#
dirt_narrow_mc_00,96,96,32,32#
dirt_narrow_hl_00,0,96,32,32#
dirt_narrow_hc_00,32,96,32,32#
dirt_narrow_hr_00,64,96,32,32#

Yes, really untidy, but its the bare basics, I didn't want to put in like 2 megabytes of code, for a tiny thing I wasn't sure will work :O
Last edited on
closed account (DSLq5Di1)
1
2
3
#include <string>
#include <fstream>
using namespace std;

Move these lines out of mapreader.cpp and into mapreader.h.
Last edited on
Thanks, once again, I had a few day delay but now I am on my laptop all set up and ready just about to set up my environment for the development of my project :)
Soon I will be able to test if it works.
Meanwhile, if it is not too hard for you, can you explain me what is the difference?
I mean, I never really had a chance to use multiply files, nor could find any info about using multiply files in my projects... and logically it makes sense to me to include the headers to the file that defines the functions, and not to the definition of the class. so again, what is the difference?

EDIT:
Ok, I set everything up, and ran it... same problem, here is the error pop-up:
Unhandled exception at 0x774415ee in Project SIGOL.exe: 0xC0000005: Access violation reading location 0xccccccc0.


Oh also forgot to mention, if I run the above code as is, it give me this error:
1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall map::~map(void)" (??1map@@QAE@XZ) referenced in function _SDL_main
1>D:\Dropbox\Project Sigol\Source Code\Project SIGOL\Debug\Project SIGOL.exe : fatal error LNK1120: 1 unresolved externals

of course I didn't understand the problem, so I did a quick fix by de-commenting the inline declaration in "mapreader.h" to:
~map() { if(sprites != NULL) delete [] sprites;}
Last edited on
closed account (DSLq5Di1)
Files are compiled seperately, so "mapreader.h" needs to include the headers of any types you declare, else how would it know what an ifstream is?

unresolved external symbol "public: __thiscall map::~map(void)"
This means the deconstructor for map has been declared but not defined, uncommenting the line as you did, defines a body and fixes your error.

warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs
http://www.gamedev.net/topic/486027-fixed-warning-lnk4098-defaultlib-msvcrtlib-conflicts-with-use-of-other-libs/

http://forums.libsdl.org/viewtopic.php?t=6313
http://stackoverflow.com/questions/115813/how-to-statically-compile-an-sdl-game-on-windows
Thank's sloppy9 :)
I tried what you told me, if I understood right, it was due to SDL being a MT DLL and included as a MT Debug DLL... so I recompiled the SDL and set my project to MT DLL and it seemed to fix the problem, then it told me it has a problem with dxgui.dll or something like that, so I reinstalled DxSDK and included it (I guess I forgot...)
Now I get another linker error, LNK2019 here:
1>main.obj : error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: char const & __thiscall std::_String_const_iterator<char,struct std::char_traits<char>,class std::allocator<char> >::operator*(void)const " (??D?$_String_const_iterator@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEABDXZ)
1>mapreader.obj : error LNK2001: unresolved external symbol __imp___CrtDbgReportW

I looked up a little on google and it seems like it has a problem with me defining a string or something like that... I don't really have a lot of time to check up on it I'll do it later, hopefully you will give me at least a direction, because I start getting uncomfortable asking for every little error I get :\
closed account (DSLq5Di1)
I don't have any experience with SDL.. but I gather those linker errors are caused by compiling your project in debug mode, whilst linking to the CRT release dll.

It may be better to compile debug/release versions of the SDL then link to the appropriate lib in your project for whichever mode you are in.
Topic archived. No new replies allowed.