Вопрос

Okay so I have to create a header file, an implementation file, and the main program, all of which are seen below to read from a file and compute certain things from it (not on to that part yet)

In my main program, you can see that I have attempted to open a file into a vector object, but for some reason, it doesn't seem to work returning this error: error: base operand of ‘->’ has non-pointer type ‘std::ifstream {aka std::basic_ifstream<char>}’

Once its loaded into a vector object, I have to compute certain types of info from it, such as the month with the most amount of rain etc.

I've overloaded the input stream extraction in the header file and implemented this sort of in the implementation file.

Also if there's any suggestions on how to improve the code, please do tell, thanks

del

Это было полезно?

Решение 2

If I've got what you want to do right:

  1. Open a file.
  2. Read some data in about a month's weather and details.
  3. Do some processing on it which you haven't written yet.

First off:

ifstream file("meteorogical.data", ios::out);

As long as we assume the file exists, you're wanting to read from the file.

The option you're putting on the end - ios::out sets the file to be written to, not read. (see here) You don't want to pass this parameter in at all. Because your stream is ifstream (InputFileStream), it defaults to the read in (which is ios::in)

Secondly, you're checking if it's not open

!file.is_open() whereas you only want to read from it if it is open

Third:

file->MonthData += line + '\n';

This is probably the worst line. Firstly, your missing the difference between a class definition and a class instance. I'd recommend having a walkthrough of some basic C++ tutorials, but this one describes classes. The main issue here is your MonthData is a class definition. That is, a blue-print of what your Month should contain, separate to this is an instance of that Month, which would specify values for a particular month and particular weather details (ie. it was sunny in march)

You've written the definition fairly well, but you still need to create the instances. Something like MonthData myCurrentMonth(); before you open the file. (You do this already with the string line;)

Still on this line - what I assume you're trying to do based off the istream & operator>> in your class definition is to say file >> myCurrentMonth. But lets ignore that, because you don't need to do it that way, it wont work with your current code and its overly complex. Instead, don't worry about the MonthData object until after you're finished with getting all the data from file. Store all the lines in that string, close the file, then create the MonthData.

Something like:

string theWholeFileText;
while (getline(file, line))
{
    theWholeFileText = theWholeFileText + line + '\n';
}
file.close();

Once you've got the whole text of the file, you can parse it how you like to find your month data. I don't have your file, so you'll need to retrieve the relevant data, but at the end you should have all the info you need to pass into a new MonthData instance.

int y, int m, float tmax, float tmin, int af, float r, float s; 
//Parsing these values from the theWholeFileText

//Plug these values into a new Class Instance of the MonthData.
MonthData monthFromFile(y, m, tmax, tmin, af, r, s);

Then finally your processing. You mentioned to do it within istream & operator>>, but I'd suggest having a void MonthData::Process() function that you call once you've got all the data in.

It'd look like:

MonthData::Process(){
    //process data here.
}

NB: I've mentioned MonthData throughout, but in some places you've miss-spelled it as MnthData.

Другие советы

Your code sample is a collection of errors. These are the first three I came across, in no particular order. I am sure there are more.

First, file is not a pointer, so you need

file.member

not

file->member

Second, std::ifstream has no member MonthData. So you can't do this

file->MonthData ....

or even

file.MonthData ....

Third, you seem to have MnthData in some places, and MonthData in others.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top