Need help overloading an operator

So im doing this assignment for my C++ class, and we have to sort objects based on a certain private data member. I have gotten it down to one error, and I cannot get it right for the life of me.
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
#include <iostream>
#include <string>
#include <algorithm>


using namespace std;

class Student
{
private:

	int stuID;
	string stuName;
	char stuGender;
	string stuDOB;
	string stuMajor;
	int stuCredits;
	double stuGPA;

public:

	void setStudent(int i, string n, char g, string d, string m, int c, double p)
	{
		stuID = i;
		stuName = n;
		stuGender = g;
		stuDOB = d;
		stuMajor = m;
		stuCredits = c;
		stuGPA = p;
	}

	int getCredits() const
	{
		return stuCredits;
	}

	void getStudent(){}
};

bool sortingMethod(const Student& credit1, const Student& credit2)
{
	return credit1.getCredits() < credit2.getCredits();
}


int main()
{
	
	Student s1, s2, s3, s4;
	Student aStudents[4] = {s1, s2, s3, s4};

	s1.setStudent(6984, "John Trovota", 'M', "1/1/1980", "CIS", 64, 3.75);
	s2.setStudent(2323, "Britney Speer", 'F', "5/23/1984", "BIS", 78, 2.98);
	s3.setStudent(1452, "Kathy Johnson", 'F', "12/25/1982", "CIS", 30, 3.33);
	s4.setStudent(4321, "Bill Newton", 'M', "7/8/1976", "BIS", 100, 3.95);

	sort(aStudents, aStudents + 4, sortingMethod);

	for(int i; i < 4; i++)
	{
		cout << aStudents[i] << endl;
	}

	return 0;
}


The error messege is:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Student' (or there is no acceptable conversion)

The error comes up in the for-loop at the end of the main function. Can anyone help?
closed account (zwA4jE8b)
The error is because you are telling cout to output the class object and it doesn't know how to handle that. you need to write what members you want to output, if its everything then you can

1
2
3
4
for(int i; i < 4; i++)
	{
		cout << aStudents[i]."member" << endl << aStudents[i]."next member" << ...and so on << endl;
	}


see the difference, I am telling it to cout members of the class which are datatypes that cout can handle.

Now if you want to create an overloaded operator function in your class, read this http://cplusplus.com/doc/tutorial/classes2/

the overloaded operator will tell cout (or other ostream) how to handle '<<'
Last edited on
Yea i see the difference. I have been getting some help from DaniWeb, but they stopped responding. Here is my revised code so far:
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
#include <iostream>
#include <string>
#include <algorithm>



using namespace std;

class Student
{
private:

	int stuID;
	string stuName;
	char stuGender;
	string stuDOB;
	string stuMajor;
	int stuCredits;
	double stuGPA;

public:

	void setStudent(int i, string n, char g, string d, string m, int c, double p)
	{
		stuID = i;
		stuName = n;
		stuGender = g;
		stuDOB = d;
		stuMajor = m;
		stuCredits = c;
		stuGPA = p;
	}

	int getCredits() const
	{
		return stuCredits;
	}

	friend ostream & operator<<(ostream & stream, Student &);

};

bool sortingMethod(const Student& credit1, const Student& credit2)
{
	return credit1.getCredits() < credit2.getCredits();
}

ostream & operator<<(ostream & stream, Student & out)
{
	stream << out;
	return stream;
}


int main()
{
	
	Student s1, s2, s3, s4;
	Student aStudents[4] = {s1, s2, s3, s4};

	s1.setStudent(6984, "John Trovota", 'M', "1/1/1980", "CIS", 64, 3.75);
	s2.setStudent(2323, "Britney Speer", 'F', "5/23/1984", "BIS", 78, 2.98);
	s3.setStudent(1452, "Kathy Johnson", 'F', "12/25/1982", "CIS", 30, 3.33);
	s4.setStudent(4321, "Bill Newton", 'M', "7/8/1976", "BIS", 100, 3.95);

	sort(aStudents, aStudents + 4, sortingMethod);

	for(int i=0; i < 4; i++)
	{
		 cout << aStudents[i] << endl;
	}

	return 0;
}


Still cant seem to overload the << operator
On line 50, you have to actually do the work. It doesn't know how to send a Student object (out) to the stream. Print the members or whatever there.

Your situation is like having a recipe for cornbread that contained, "Step 1: Make cornbread."
Last edited on
Thats just it. I dont know how to do the work. We havent covered this in class yet. The guy over at DaniWeb used an initialization list in his example, then used the code in line 50 to read
stream << out.bar;

bar was the object of type foo that was initialized inside the class. I tried doing that to no avail
Here's a list of members that you could be printing...
1
2
3
4
5
6
7
	int stuID;
	string stuName;
	char stuGender;
	string stuDOB;
	string stuMajor;
	int stuCredits;
	double stuGPA;


stream << out.stuID << endl;
Took your advice... coded the stream section to include out.datamember (replace datamember with the private data member)... and all it gave me was large negative numbers and special characters. I do not know what is wrong
closed account (zwA4jE8b)
lets see your code
Last edited on
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
#include <iostream>
#include <string>
#include <algorithm>



using namespace std;

class Student
{
private:

	int stuID;
	string stuName;
	char stuGender;
	string stuDOB;
	string stuMajor;
	int stuCredits;
	double stuGPA;

public:

	void setStudent(int i, string n, char g, string d, string m, int c, double p)
	{
		stuID = i;
		stuName = n;
		stuGender = g;
		stuDOB = d;
		stuMajor = m;
		stuCredits = c;
		stuGPA = p;
	}

	int getCredits() const
	{
		return stuCredits;
	}

	friend ostream & operator<<(ostream & stream, Student &);


};



bool sortingMethod(const Student& credit1, const Student& credit2)
{
	return credit1.getCredits() < credit2.getCredits();
}

ostream & operator<<(ostream & stream, Student & out)
{
	stream << out.stuID << out.stuName << out.stuGender << out.stuDOB << out.stuMajor << out.stuCredits << out.stuGPA << endl;
	return stream;
}


int main()
{
	
	Student s1, s2, s3, s4;
	Student aStudents[4] = {s1, s2, s3, s4};

	s1.setStudent(6984, "John Trovota", 'M', "1/1/1980", "CIS", 64, 3.75);
	s2.setStudent(2323, "Britney Speer", 'F', "5/23/1984", "BIS", 78, 2.98);
	s3.setStudent(1452, "Kathy Johnson", 'F', "12/25/1982", "CIS", 30, 3.33);
	s4.setStudent(4321, "Bill Newton", 'M', "7/8/1976", "BIS", 100, 3.95);

	sort(aStudents, aStudents + 4, sortingMethod);

	for(int i=0; i < 4; i++)
	{
		 cout << aStudents[i] << endl;
	}

	return 0;
}


sorry if i am being a big noob, this is just new to me. but thank you guys for all your help
closed account (zwA4jE8b)
1
2
3
4
aStudents[0].setStudent(6984, "John Trovota", 'M', "1/1/1980", "CIS", 64, 3.75);
	aStudents[1].setStudent(2323, "Britney Speer", 'F', "5/23/1984", "BIS", 78, 2.98);
	aStudents[2].setStudent(1452, "Kathy Johnson", 'F', "12/25/1982", "CIS", 30, 3.33);
	aStudents[3].setStudent(4321, "Bill Newton", 'M', "7/8/1976", "BIS", 100, 3.95);


and format your output better. don't apologize for not knowing, the important thing is that you're trying.

but you should do...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	Student aStudents[4];

	aStudents[0].setStudent(6984, "John Trovota", 'M', "1/1/1980", "CIS", 64, 3.75);
	aStudents[1].setStudent(2323, "Britney Speer", 'F', "5/23/1984", "BIS", 78, 2.98);
	aStudents[2].setStudent(1452, "Kathy Johnson", 'F', "12/25/1982", "CIS", 30, 3.33);
	aStudents[3].setStudent(4321, "Bill Newton", 'M', "7/8/1976", "BIS", 100, 3.95);

	//sort(aStudents, aStudents + 4, sortingMethod);

	for(int i=0; i < 4; i++)
	{
		 cout << aStudents[i] << endl;
	}

	cin.get();
	return 0;
}


because the array is of student objects, so no need to declare them twice.

however if you ad them to the array after you init them, then your current code would work

Last edited on
EDIT: Nevermind! It worked! Thank you guys so much. I realize now that adding the objects to the array before init'ing them caused some weird issues. Also thanks alot with the operator overloading. I understand it now a lot better.
Last edited on
closed account (zwA4jE8b)
oh you should uncomment that out. I was just not using it when trying to figure out what was wrong
EDIT: Nevermind! It worked! Thank you guys so much. I realize now that adding the objects to the array before init'ing them caused some weird issues. Also thanks alot with the operator overloading. I understand it now a lot better.


The code you had made copies of s1-s4 in the array. Glad to hear it's working out.
Topic archived. No new replies allowed.