Error : a nonstatic member reference must be relative to a specific object

Hello guys, I wrote this and I'm getting the Error : "a nonstatic member reference must be relative to a specific object." Read about it, did not seem to quite understand it. Is there anybody willing to spoon feed me the answer? I am not that experienced with programming but I don't see something wrong with my syntax? "FILE" is not the built in function.
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 #include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

class ReadFile
{
public:
	// Function Declarations
	string FileReader(string fileName);
	string ExtractBody(const string& text, string tag);
	void ExtractTags(string& text);
private:
	const string filename;
	const string delimeters;
	string body;
	int count = 0;
};

string ReadFile::FileReader(string fileName)
{
	string body; 
	char ch; 
	ifstream FILE(fileName); 
	if (!FILE)
	{
		cerr << fileName << " not found!" << endl; 
		exit(1); 
	}
	while (FILE.get(ch))
	{
		body = body + ch; 
		FILE.close(); 
		return body; 
	}
}

string ReadFile::ExtractBody(const string& text, string tag)
{
	string body; 
	unsigned int pos = 0, start; 
	while (true)
	{
		start = text.find("<" + tag, pos); 
		if (start == string::npos)
		{
			return body; 
			start = text.find(">", start); 
			start++; 
		}
		pos = text.find("</" + tag, start); 
		if (pos == string::npos)
		{
			body = text.substr(start, pos - start); 
			return body; 
		}
	}
}

void ReadFile::ExtractTags(string& text)
{
	unsigned int start = 0, pos; 
	while (start < text.size())
	{
		start = text.find("<", start); 
		if (start == string::npos)
		{
			break; 
		}
		pos = text.find(">", start); 
		if (pos == string::npos)
		{
			break; 
		}
		text.erase(start, pos - start + 1); 
	}
}

int main()
{
	string file; 
	string bracket; 
	bracket = "BODY"; 
	bool ExtractotherTags = true; 
	string Body = ReadFile::FileReader(file);  // Error : a nonstatic member reference must be relative to a specific object
	string all = ReadFile::ExtractTags(Body, bracket); //Error : a nonstatic member reference must be relative to a specific object
	for (string s : all)
	{
		if (ExtractotherTags)
		{
			ReadFile::ExtractTags(s); // Error : a nonstatic member reference must be relative to a specific object
			cout << s << '\n';
		}
	}
}
Last edited on
you need an object ;)
you need this:
ReadFile foo;
...

foo.ExtractTags(s);

you can't just call class functions in c++, unless they are static functions which can only tinker with static variables -- its a specialized use case, really. You CAN have stand alone functions that don't need a class. Do you require an object at all here, or is this left over java code?
Last edited on
1
2
3
4
5
6
while (FILE.get(ch))
	{
		body = body + ch; 
		FILE.close(); 
		return body; 
	}


I think you mean:

1
2
3
4
5
6
while (FILE.get(ch))
{
	body += ch; 
}

return body; 


1
2
3
4
5
6
if (start == string::npos)
		{
			return body; 
			start = text.find(">", start); 
			start++; 
		}


the 2 lines following return body will never be executed!
Last edited on
This compiles. You'll now need to debug to find the problems. Why put the functions as part of a class? The class doesn't use any private members - the specified private members aren't being used!

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

class ReadFile
{
public:
	// Function Declarations
	string FileReader(const string& fileName);
	string ExtractBody(const string& text, const string& tag);
	void ExtractTags(string& text);
};

string ReadFile::FileReader(const string& fileName)
{
	string body;
	ifstream FILE(fileName);

	if (!FILE) {
		cerr << fileName << " not found!" << endl;
		exit(1);
	}

	for (char ch; FILE.get(ch); body += ch);

	return body;
}

string ReadFile::ExtractBody(const string& text, const string& tag)
{
	string body;
	size_t pos {}, start {};

	while (true) {
		start = text.find("<" + tag, pos);
		if (start == string::npos) {
			// THIS IS NOT RIGHT
			return body;
			start = text.find(">", start);
			start++;
		}

		pos = text.find("</" + tag, start);
		if (pos == string::npos) {
			body = text.substr(start, pos - start);
			return body;
		}
	}
}

void ReadFile::ExtractTags(string& text)
{
	size_t start {}, pos {};

	while (start < text.size()) {
		start = text.find("<", start);
		if (start == string::npos)
			break;

		pos = text.find(">", start);
		if (pos == string::npos)
			break;

		text.erase(start, pos - start + 1);
	}
}

int main()
{
	const string bracket {"BODY"};
	const string file {"myfilename"};

	bool ExtractotherTags {true};
	ReadFile rf;

	const string Body {rf.FileReader(file)};
	const string all {rf.ExtractBody(Body, bracket)};

	istringstream iss(all);

	for (string s; iss >> s; )
		if (ExtractotherTags) {
			rf.ExtractTags(s);
			cout << s << '\n';
		}
}

Last edited on
I didn't realize I did not use an object, sorry for that.
@seeplus the code is not done yet, there are still things that need to be implemented. They private class members are not being used YET. Still on the trial run. But I believe I have been in progress since my first forum xD.
Topic archived. No new replies allowed.