Basic Queue exercise: error when assigning size() of a deque

Hello, I am working to get the capacity of a Queue data structure as per the class outlined below. I attempted to assign size() to the deque variable, but I am getting this error:
Compilation failed due to following error(s).main.cpp: In constructor ‘CharQueue2::CharQueue2(size_t)’:
main.cpp:26:23: error: lvalue required as left operand of assignment
vardeque.size() = size;

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
//Goal:Given the class CharQueue2 below, implement needed code to make each
//method functional.
#include <deque>
#include <iostream>

class CharQueue2 
{
    public:
        CharQueue2();
        CharQueue2(size_t size); //<== I am working on this
        void enqueue(char ch);
        char dequeue();
        bool isEmpty() const;
        void swap(CharQueue2 & src);
        size_t capacity() const; //<=== and this 
    private:
        std::deque<char> vardeque;
};

CharQueue2::CharQueue2 () 
{
    vardeque = {};
}

//Goal is to set size of deque here
CharQueue2::CharQueue2(size_t size) 
{
    vardeque.size() = size;
}

//Then I use max_size() to get the capacity of such deque.
size_t CharQueue2::capacity() const {
    return vardeque.max_size();
}


void CharQueue2::swap(CharQueue2 & src) 
{
    CharQueue2 tmp_var = src;
    src = *this;
    *this = tmp_var;
}

//Implement enqueue; this will add a character in the Queue
void CharQueue2::enqueue (char ch) 
{
    vardeque.push_back(ch);
}


int main()
{
    //Create an object and set size
     CharQueue2 q2(10); 
    //Next step is to call method enqueue and pass argument
    std::cout << q2.capacity();

     
    return 0;
}
1
2
3
4
5
//Goal is to set size of deque here
CharQueue2::CharQueue2(size_t size) 
{
    vardeque.size() = size;
}


size() is a function that GIVES you the size of the queue. Doesn't set it.

You want this function:
http://www.cplusplus.com/reference/deque/deque/resize/

vardeque.size() = size;

you can't do that.
vardeque is a stl container and it manages its own size, and you cannot tamper with it (without hackery which would be undefined behavior and likely cause instability).

also, functions are not working this way. you cannot assign a value to a function call, eg foo() = x ... the other way works for non-void functions eg x = foo(); is ok because x is a variable, and variables can be assigned. An l-value means a value that can be on the left side of an assignment, and a function call isnt one, that is what the message is trying to say.


Last edited on
Since you're using the std::deque as your container you should know that deque.size() is an output function, it returns the current size of the container. If you want to try to change the size of the container you will probably want to look at the resize() function instead.

Perfect. resize() does work just fine. THANK YOU !!!
Topic archived. No new replies allowed.