I am doing this assignment for my C++ class, and we have to instantiate 4 objects of the Student class, set the data members and then sort them according to the GPA variable. Ive whittled down the errors to just one, and I am stumped. Here is the error:

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

It happens in the for-loop at the end of main... anyone please help?

#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 stream class is made for built-in types like int and double it has no idea what a student is. If you want to output it to the stream using cout you will need to write a function that does so. To do that you need to overload the operator <<. Here is a simple example to look at.

#include<iostream>

using namespace std;

class foo
{
private:
    int bar;
public:
    foo() : bar(5) {}
    ~foo() {}
    friend ostream & operator<<(ostream &, foo &)
};

ostream & operator<<(ostream & stream, foo & out)
{
    stream << out.bar;
    return stream;
}

int main()
{
    foo bar;
    cout << bar;
    return 0;
}

Edited 5 Years Ago by NathanOliver: n/a

Thank you very much! however, i do not understand this part of the code:

foo() : bar(5) {}

what does that do besides create a constructor?

So in my class, I could use the initialization list to set the data for the 4 objects, then change this code:

friend ostream & operator<<(ostream &, foo &)
};
 
ostream & operator<<(ostream & stream, foo & out)
{
    stream << out.bar;
    return stream;
}

to be what I need it to be? That seems a little odd, initializing 4 objects inside the actual class.

Sorry for the double post, but this is where I am so far. I just dont really understand this.

#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;
}
This article has been dead for over six months. Start a new discussion instead.