Sign in to follow this  

getline() does not like the ifstream I put into it

This topic is 3634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

void LoadLevel(std::string Filename, Level* LevelStruct)
{
	std::ifstream FileStream(Filename.data());
	std::string CurrentLine;
	std::getline(FileStream,CurrentLine.data()); //<- Line 9
	FileStream.close();
	return;
};



Shouldn't the above code work? I've seen ifstream's put into getline elsewhere! I'm getting the errors:
Quote:
level loading functions.h(9) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream' string(528) : see declaration of 'std::getline' level loading functions.h(9) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'const char *' string(528) : see declaration of 'std::getline' level loading functions.h(9) : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided string(476) : see declaration of 'std::getline'

Share this post


Link to post
Share on other sites
Don't use the "data()" member of std::string. Use c_str() instead.

Don't write "return" without a value, functions do that by themselves ;)

Pass complex objects by const reference.

Don't pass pointers unless you want an optional argument, use references.

Don't close() std::fstreams, they are RAII objects.

Don't call a variable SomeStruct: the fact that it is a struct (or class) is irrelevant and verbose. I make variables lowerCamelCase and types UpperCamelCase so I can say "Level &level".


void LoadLevel(const std::string &filename, Level &level)
{
std::ifstream fileStream(filename.c_str());
std::string currentLine;
std::getline(fileStream,currentLine);

// because level is a reference
// use level., not level-> for member access
// use level, not *level to treat it as a value
};

Share this post


Link to post
Share on other sites
omg("thanks" * ONE_MILLION);

That really helped.
Also, thanks for the grammar tips :]

Quote:
Original post by rip-off
Pass complex objects by const reference.

Don't pass pointers unless you want an optional argument, use references.


Oh, can you explain these two in greater detail (or give me a link)?

Share this post


Link to post
Share on other sites
Quote:
Original post by justcallmedrago
Quote:
Original post by rip-off
Pass complex objects by const reference.

Don't pass pointers unless you want an optional argument, use references.


Oh, can you explain these two in greater detail (or give me a link)?


[8] References, C++ FAQ Lite

In particular, note section [8.6] on that page.

Share this post


Link to post
Share on other sites

This topic is 3634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this