Hi--
I am having trouble creating a list in C++ where each element of the list is an object of class credit. Looks to me like the program is having trouble with defining the != and > operators for this class. Would appreciate it if someone could take a look please.
The error message I get: C:\Program Files\DevStudio\VC\INCLUDE\functional(69) : error C2678: binary '!=' : no operator defined which takes a left-hand operand of type 'const class credit' (or there is no acceptable conversion)
C:\Program Files\DevStudio\VC\INCLUDE\functional(75) : error C2678: binary '>' : no operator defined which takes a left-hand operand of type 'const class credit' (or there is no acceptable conversion)
Error executing cl.exe.
This is the main routine:
#define NR_END 1
#define FREE_ARG char*
#define NRANSI
#define MAXITS 200
#define TOLF 1.0e-4
#define TOLMIN 1.0e-6
#define TOLX 1.0e-7
#define STPMX 100.0
#define TINY 1.0e-20
#define ALF 1.0e-4
#define EPS 1.0e-4
#define MAX_WIDTH 200
int main()
{
char filename[MAX_WIDTH + 1]="RTM.txt";
mymatrix readmat;
int i;
credit thiscredit;
list<credit> creditlist;
list<credit>::iterator credit_ptr;
readmat = read("creditdata.txt",-1,0); //routine that reads data. works
credit_ptr = creditlist.begin();
for(i=1;i<=readmat.m;i++) //loop through the rows of the readmat matrix
{
thiscredit.id = readmat.p[i][1]; //assign value
thiscredit.rating = readmat.p[i][2]; //assign value
creditlist.insert(credit_ptr,thiscredit); //insert into the list
credit_ptr++; //increment credit pointer
}
return 0;
}
This is my class creditin the "Myheader.h file:"
class credit{
public:
int id;
int rating;
credit(){};
credit(int id1, int rating1){id=id1;rating=rating1;}
credit(const credit &src){cout << "Now calling credit copy constructor" << endl; id=src.id; rating=src.rating;}
bool operator==(const credit &src)
{
return (id==src.id);
}
bool operator!=(const credit &src)
{
return (id != src.id);
}
bool operator>(const credit &src)
{
return (id > src.id);
}
bool operator<(const credit &src)
{
return (id < src.id);
}
credit &operator=(const credit &src)
{
id = src.id;
rating = src.rating;
return *this;
}
friend ostream &operator<<(ostream &os, credit &src)
{
os << "ID: " << src.id << " Rating: " << src.rating << endl;
return os;
}
};
I have always used char.
Very roughly: operators are signs like +, -, * etc which are defined for normal variables the way we define them always. However, when you define objects e.g. matrix, the you also need to tell the compiler what matrixA + matrixB means. And so you have to define these operations for the compiler.