Hi,
Firstly, many thanks in advance for any help or advice, it is very much appreciated.
I am trying to overload the << operator to output the data members 'loan' and 'name' for my student class.
Here is my setup (i have kept it very, very simple, in hope of a clear response). It might seem like a lot, but it is just 2 set methods and two get methods.
Code:
using namespace std;
Student::Student()
{
loan = 0;
name = "";
}
Student::Student(int extra)
{
loan = extra;
name = "";
}
void Student::setLoan(int amount)
{
loan += amount;
}
void Student::setName(string word)
{
name = word;
}
int Student::getLoan()
{
return loan;
}
string Student::getName()
{
return name;
}
Student::operator<< (ostream &out, Student)
{
Student studentn;
out << studentn.getName();
out << studentn.getLoan();
return studentn;
}
Here is my header file:
Code:
class Student
{
private:
int loan;
string name;
public:
Student();
Student(int);
void setLoan(int);
int getLoan();
void setName(string);
string getName();
Student operator<< (ostream &out, Student);
};
This is my main function:
Code:
void main()
{
int tmpLoan = 300;
string tmpName = "Bruno";
Student bruno (100);
bruno.setLoan(tmpLoan);
bruno.setName(tmpName);
//cout << bruno.getLoan() << endl;
//cout << bruno.getName() << endl;
cout << bruno;
}
I am getting the following errors:
c:\visual studio projects\studentoverloader\student.h(18) : error C2804: binary 'operator <<' has too many parameters
c:\visual studio projects\studentoverloader\student.cpp(41) : error C2556: 'int __thiscall Student:
perator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Student)' : overloaded function differs only by return type from 'c
lass Student __thiscall Student:
perator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Student)'
c:\visual studio projects\studentoverloader\student.h(18) : see declaration of '<<'
c:\visual studio projects\studentoverloader\student.cpp(41) : error C2371: '<<' : redefinition; different basic types
c:\visual studio projects\studentoverloader\student.h(18) : see declaration of '<<'
main.cpp
I really really would appreciate any help at all, it is not urgent, or for anything in particular, i am just stuck.
Best regards, global