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 credit in 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;
}
};
This error happens when you invoke a NON-CONSTANT class member function on a class object that has been declared constant.
This is because if a member function has not been declared constant, it implies that the function
will possibly modify the object - and you are not allowed to modify a constant object.
class myclass
{
public:
//Note that this member function has NOT been declared const
booloperator> (const myclass& other)
{
returntrue;
}
//Note that this member function has been declared constant
booloperator< (const myclass& other) const
{
returntrue;
}
};
int main()
{
const myclass am; //constant object
myclass bm; //non-constant object
//This next check will create an error because
//we will be calling the NON-CONSTANT member function operator>
//on object am but am has been declared a constant object.
if (am > bm)
cout << " am is greater than bm" << endl;
//ok
if (bm > am)
cout << " bm is greater than am" << endl;
//This is ok because the operator< member function has been declared constant
if (am < bm)
cout << " am is less than bm" << endl;
//ok
if (bm < am)
cout << " bm is less than am" << endl;
}
In your case - Make the member functions constant - because there is no reason why
the != or > operator overload functions need to modify the objects on which they have been invoked.