Question regarding comparing two linked lists

This is a discussion on Question regarding comparing two linked lists within the C++ Programming forums, part of the General Programming Boards category; Hi, I am encountering a compiler error that I am not able to resolve. I wrote a simple test program ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167

    Question regarding comparing two linked lists

    Hi,
    I am encountering a compiler error that I am not able to resolve. I wrote a simple test program to demonstrate it:
    Code:
    #include <iostream>
    #include <list>
    
    using std::cout;
    using std::endl;
    
    using std::list;
    
    class TestClass {
            public:
                    TestClass(int a) { somedummydata = a; }
    
                    bool operator==(const TestClass &other) { return (somedummydata == other.somedummydata); }
                    bool operator!=(const TestClass &other) { return (somedummydata != other.somedummydata); }
    
            private:
                    int somedummydata;
    };
    
    int main() {
            list<TestClass> *list1 = new list<TestClass>;
            list<TestClass> *list2 = new list<TestClass>;
    
            for (unsigned int i = 0; i < 10; ++i) {
                    TestClass a(i);
                    list1->push_back(a);
                    list2->push_back(a);
            }
    
            if (*list1 == *list2) {
                    cout << "a" << endl;
            }
    }
    the code does not compile. Here is the compiler output:
    Code:
    /usr/include/c++/4.1.3/bits/stl_list.h: In function ‘bool std::operator==(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&) [with _Tp = TestClass, _Alloc = std::allocator<TestClass>]’:
    test.cpp:30:   instantiated from here
    /usr/include/c++/4.1.3/bits/stl_list.h:1174: error: passing ‘const TestClass’ as ‘this’ argument of ‘bool TestClass::operator==(const TestClass&)’ discards qualifiers
    I don't quite understand the error. Can someone please tell me what I am doing wrong?

    The output is from GCC 4.1 on Linux.

    Thank you very much

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1) Why are you allocating the lists on the heap?

    2) You need to make the comparison members const:
    Code:
    bool operator==(const TestClass &other) const { return (somedummydata == other.somedummydata); }
    This allows them to be called on const instances of the class. Since list's == is const, the list is const within the operator, and thus its members are const too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    1) Why are you allocating the lists on the heap?
    I am having this problem on a large project. This is just the test code I wrote to demonstrate my question. On my original project, allocating the lists on the heap allows more flexibility.
    Code:
    bool operator==(const TestClass &other) const { return (somedummydata == other.somedummydata); }
    This allows them to be called on const instances of the class. Since list's == is const, the list is const within the operator, and thus its members are const too.
    That works perfectly, thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question regarding linked lists.
    By IXxAlnxXI in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2007, 12:54 AM
  2. Oy, question on linked lists
    By tammo21 in forum C Programming
    Replies: 8
    Last Post: 08-18-2002, 05:14 PM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21