Sign in to follow this  

Reading From a Text File

This topic is 3299 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

Hi, I've come across a problem and was wondering what is the problem with following code and how can it be corrected? Please bare in mind I've chopped out loads of code to simplify the problem, hopefully all the required information is present
//-----------------------------------
// Main File
//-----------------------------------

// create file pointer
ifstream getData;

// create pointer to file pointer object
ifstream* pGetData = &getData;

//------------------------------------
// Text loader
//------------------------------------

// Create temporary string for loading in data
string strLine = "";

// Return pointer to the beginning of the file and clear the end of file flag
pGetData->seekg(NULL, ios::beg);
pGetData->clear();

//Problematic piece of code*******
while(getline(pGetData, strLine, '\n')
{
}

The problem appears to stem from passing the pointer pGetData into the line while(getline()). I recieve the following error and do not understand why error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided 1> C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\string(527) : see declaration of 'std::getline' Any help would be much appreciated, cheers!

Share this post


Link to post
Share on other sites
You should have tried to make the smallest COMPLETE example that exhibits the problem. I noticed at least one error though. You make a pointer to an ifstream object and when you use it in getline you didn't dereference it. try:

while( getline(*pGetData, strLine, '\n') )
{
}

You also left off a trailing parenthesis after the getline function. That could have been a typo when you removed all of the code you didn't think was important.

edit: I'm pretty sure that that was the problem. see, when you passed in the pointer the compiler was expecting an object that was templated on the type

std::basic_istream<_Elem,_Traits>

That is, the argument needed to be dependant on the template arguments _Elem and _Traits. The pointer did not have that dependancy and so you got the error which MSDN describes as occuring when: "A function template has too few arguments."

Templates is tricky business.

Share this post


Link to post
Share on other sites
Ah yes dereferencing the pointer fixed the problem, the missing parenthesis was a typo Cheers.

At the risk of sounding stupid, but wanting to fully understand the problem. Why did the pointer need to be dereferenced?

Share this post


Link to post
Share on other sites
Quote:
Original post by Slyfox
At the risk of sounding stupid, but wanting to fully understand the problem. Why did the pointer need to be dereferenced?
It isn't a problem per se, but I find it a bit icky to be holding std::library classes by pointer. I also can't say that I have ever had the need to, so I question whether you actually need to here...

Share this post


Link to post
Share on other sites
Ah ok, thanks very much all.

I was trying to use a pointer as I was passing the object between many different functions and I was under the impression that a pointer was best suited to that? It was originally just passed as a reference instead of a pointer in the code I was looking at, would you suggest just sticking with that?

Share this post


Link to post
Share on other sites

This topic is 3299 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