Subclass unable to access member function

Good day,

I've been setting up my datastructure as such: I have one class 'Storage' and a subclass 'StorageMap'.

The header files of both (constructors and non-relevant stuff removed):

Storage.h
1
2
3
4
5
6
7
8
9
class Storage {

public:
	virtual void loadFile();

protected:
	char* filename;
	std::ifstream file;
};


StorageMap.h
1
2
3
4
5
6
7
8
9
#include "Storage.h"

class StorageMap : public Storage {

	public:
		void loadFile();

	private:
};


What I try to do is that StorageMap is a subclass of Storage. This seems to be the case. Now, I have a protected variable 'std::ifstream file;' in Storage, which StorageMap should also be able to access (right?).

I try to open the file using it's filename (which works I think) and then read it line-by-line using 'while end of file not reached' but I can't use 'while (!file.eof)': error C2276: '!' : illegal operation on bound member function expression

The CPP code:

Storage.cpp
1
2
3
4
5
6
7
#include "Storage.h"
//Removed constructor etc..

void Storage::loadFile() {
		printf("Error: No calling loadfile on generic storage object");
		exit(1);
}


StorageMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "StorageMap.h"

//Removed constructor etc..

void StorageMap::loadFile() {
	file.open(filename, std::ios::in );
	if (!file.is_open()) {
		printf("Error opening or finding file with filename '%s'.", filename);
		exit(1);
	}

	std::string line;
	while(!file.eof) {   //<<<<<<---BREAKS HERE
	        getline(file,line); // Save the line.
	        printf("%s\n", line);
    }
	
	file.close();
}


Am I doing something horribly wrong here? Since StorageMap is a subclass of Storage, it also has a 'file' variable it can use (right?). Why exactly doesn't this work?

Kind regards
eof is a function. try (!file.eof()) instead of (!file.eof)
Thanks, that fixed it. I was so caught up searching for the strange error message and figuring out if I had accidentally messed up a pointer that this obvious flaw went unnoticed. Thanks.
If I remember correctly, eof() has a subtle bug I believe. Try to use while (getline(file,line)) { ... } instead.

That developer spent endless nights debugging this stuff :P
Will take your advice and go with your suggestion. Thanks :)
Maybe poking my nose in unnecessarily here, but you can avoid having that implementation of Storage::loadFile() by declaring the function to be pure virtual:

1
2
3
4
class Storage {

public:
	virtual void loadFile() = 0;


The compiler will then
- require that all derived classes implement the function and
- not allow a 'Storage' object to even be instantiated

If this is what you intended, it's a great way to get the compiler to check that your classes are being used correctly.
Maybe poking my nose in unnecessarily here
By all means, poke around!

Thanks for the suggestion, I can certainly use this! I currently had a small loadFile function that said "Please use a derived class instead" and have it quit the application. This is much nicer.
Topic archived. No new replies allowed.