question on lists in C++

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

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <fstream>
#include <cfloat>
#include <cstdlib>
#include <cmath>
#include <Mynrutil.h>
#include <Mynr.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <list>

#include "Myheader.h"


using namespace std;


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.

Here is an illustration:


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
class myclass
{
public:
    
    //Note that this member function has NOT been declared const
    bool operator> (const myclass& other)
    {
        return true;
    }

    //Note that this member  function has been declared constant
    bool operator< (const myclass& other) const
    {
        return true;
    }
};


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.
Last edited on
Thanks a lot. That did it!
Topic archived. No new replies allowed.