Qt creator operator[]


This is trying my patience...

Compiler Error:

error: passing 'const task_list_class' as 'this' argument of 'task_object& task_list_class::operator[](const unsigned int&)' discards qualifiers [-fpermissive]
         if(date == task_list[x].date_of_task)


I have const correctness, can't figure out what the problem is.

Here's the object's prototype:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Stores and loads a list of tasks.  Uses the STL file
streams. */
class task_list_class{
public:
    task_list_class() : tasks({}) {}
    ~task_list_class()
    {
        for(std::vector<task_object>::iterator it = this->tasks.begin(); it != this->tasks.end();)
        {
            it = this->tasks.erase(it);
        }
    }



    /* Returns a reference to the Xth task_object
       in the list.*/
    task_object& operator[](const unsigned int&);

    /* Adds a task. */
    void add_task(const task_object& t)
    {
        this->tasks.push_back(t);
    }

    /* Removes a task.*/
    void remove_task(const unsigned int& i)
    {
        if(i < tasks.size())
        {
            if(tasks.size() > 0)
            {
                this->tasks.erase((this->tasks.begin() + i));
            }
        }
    }

    /* Returns the list of tasks. */
    std::vector<task_object> gtasks() const
    {
        return this->tasks;
    }

    /* Assigns a new unique ID to a task_object*/
    unsigned long long new_task_id()
    {
        unsigned long long n(0);
        while(this->is_id(n))
        {
              n++;
        }
        return n;
    }

    void save();
    void load();

private:
    std::vector<task_object> tasks;
    bool is_id(const unsigned long long& i)
    {
        if(i == 0)
        {
              return true;
        }
        for(std::vector<task_object>::const_iterator it = this->tasks.begin();
            it != this->tasks.end(); ++it)
        {
            if(it->id == i)
            {
                return true;
            }
        }
        return false;
    }


};


Thanks for your time.

Also, this is Qt Creator, with C++11 and MinGW.
Last edited on
closed account (o1vk4iN6)
You are trying to use the operator[] with a const task_list_class but it is not declared to be const, so it is not valid.

 
task_object& operator[](const unsigned int&) const;
I'm finding that difficult to swallow.

Could you elaborate please? C++11 allows non-const reference returns from operators...
Last edited on
closed account (o1vk4iN6)
The const at the end of the member function definitions declares the object as const.

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

class Foo
{
public:

    int& bar1() const { return foobar; } // error
    int& bar2()       { return foobar; } // valid
    const int& bar3() const { return foobar; } // valid

private:

    int foobar;

};

const Foo foo;

// this is the erro you are getting
// bar2() needs to be declared const
foo.bar2(); // error

foo.bar3(); // valid 
Last edited on
yes I know that.......

C++11 allows for it... I really don't want to spend hours digging through Qt's source code just to find 1 thing, I just want to build a calendar.

The task object emplements objects from Qt's library, so I'm assuming it has somthing to do with that??

Can anyone please help elaborate why??
Last edited on
Also, not only won't that work, but it puke up more.... on top of the discarded qualifiers...

(-,-')
closed account (o1vk4iN6)
It's not a problem with Qt... its a problem with YOUR code. You are trying to call a function that isn't declared const using an object declared const.

Your error: http://ideone.com/fe7y4t
Solution: http://ideone.com/paskOq
oh...

I feel like a moron for missing that.

I'm so sorry, I've been working on this thing all day... I guess I need to take a break now.
Last edited on
Topic archived. No new replies allowed.