"Identifier not found" error within a member function

Hi, I am still learning to build a user-defined class and use some member functions to generate a report for students' final, midtern, and homework grades. The main problem I encountered right now is when I try to call a global function (for instance the read_hw()) within a member function (for instance the record.read(cin), it won't compile and give an error message that "identifier not found". I just don't get it, cause the read_hw() was decleared right under the member function. Another error it shows is that the function ::grade(midterm, final, homework) is not a member of "global namespace". What does that even mean?

Thanks for your time and any kind of help is appreciated!

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
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>

using namespace std;

class Student_info {
private:
	string name;
	double midterm, final;
	vector<double> homework;

public:
	istream& read(istream&);
	double grade() const;
	string get_name() const {return name;}
	vector<double> get_homework() const {return homework;}
	bool valid() const {return !homework.empty();}
};

istream& Student_info::read(istream& in){
		in >> name >> midterm >> final;
		read_hw(in, homework);
		return in;
}

void read_hw(istream& in, vector<double>& records){
	if(in){
		records.clear();
		double x;
		while (in>>x){
		records.push_back(x);
		}
		in.clear();
	}
}
		
double Student_info::grade() const{
	return ::grade(midterm, final, homework);
}

double grade(double mid, double fin, vector<double> records){
	try {
		return 0.4*mid + 0.4*fin + 0.2*get_median(records);
	} catch (domain_error e){
		cout<<e.what();
	}
}

double get_median(vector<double>& records){
	if (records.size() == 0) 
		throw domain_error("there are no homework being submitted.");
	vector<double>::size_type mid = records.size()/2;
	sort(records.begin(), records.end());
	return records.size()%2 == 0? records[mid+1] : records[mid];
}

bool compare(const Student_info& x, const Student_info& y){
	return x.get_name() < y.get_name();
}

bool did_all_hw(const Student_info& s){
	return (find(s.get_homework().begin(), s.get_homework().end(), 0) == s.get_homework().end());
}

int main(){
	vector<Student_info> students;
	Student_info record;
	string::size_type maxlen = 0;
	vector<Student_info> did, didnt;

	while(record.read(cin)){
		if (did_all_hw(record))
			did.push_back(record);
		else
			didnt.push_back(record);
		maxlen = max(maxlen, record.get_name().size());
		students.push_back(record);
	}

	if (did.empty()){
		cout<<"No student did all the homework"<<endl;
	}

	if (didnt.empty()){
		cout<<"All the student did all the homework"<<endl;
	}

	sort(students.begin(), students.end(), compare);

	for (vector<string>::size_type i = 0; i != students.size(); ++i){
		cout << students[i].get_name() << string(maxlen + 1 - students[i].get_name().size(), ' ');
		try{
			double final_grade = students[i].grade();
			streamsize prec = cout.precision();
			cout<<setprecision(3)<<final_grade<<setprecision(prec);
		}
		catch (domain_error e){
			cout<<e.what();
		}
		cout<<endl;
	}
	return 0;
}
Last edited on
it's an ordering issue. put your read_hw() method implementation before you call it.

this is where header files come in handy.
You need to define the functions before you call them, otherwise the compiler doesn't know they exist. Basically, move your functions that are giving errors above the functions that call them and it should work properly.
I just don't get it, cause the read_hw() was decleared right under the member function.


That's your problem. In C++ functions have to be declared before they are called. So move read_hw() above Student_info::read() .

Another error it shows is that the function ::grade(midterm, final, homework) is not a member of "global namespace".


Remove the double colon in front of grade.
Last edited on
OP, note: it only needs to know about the methods, i.e. their prototypes.

If you had a header file like this:

1
2
3
4
5
6
7
8

class Student_info;

double Student_info::grade() const;
double grade(double mid, double fin, vector<double> records);
double get_median(vector<double>& records);
bool compare(const Student_info& x, const Student_info& y);
bool did_all_hw(const Student_info& s);


you would not have to move stuff about in your .cpp file. I think :)



In fact, if it was me, i'd move my class declararion into my header file too, so it would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class Student_info {
private:
	string name;
	double midterm, final;
	vector<double> homework;

public:
	istream& read(istream&);
	double grade() const;
	string get_name() const {return name;}
	vector<double> get_homework() const {return homework;}
	bool valid() const {return !homework.empty();}
};


double Student_info::grade() const;
double grade(double mid, double fin, vector<double> records);
double get_median(vector<double>& records);
bool compare(const Student_info& x, const Student_info& y);
bool did_all_hw(const Student_info& s);


with the appropriate #include's at the top.
Last edited on
Topic archived. No new replies allowed.