Basic Queue operation: enqueue/push_back returning an error

Hello, I am writing the "enqueue" method for the class below.
I am getting error:

main.cpp: In function ‘void enqueue(char)’:
main.cpp:21:5: error: ‘vardeque’ was not declared in this scope

Any idea what is wrong?


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
/*Goal: Exercise gave CharQueue2 class as below. Request is to implement
  the respective methods to allow operations in a Queue data structure.
*/
#include <deque>
#include <iostream>

class CharQueue2 
{
    public:
        CharQueue2();
        CharQueue2(size_t size);
        void enqueue(char ch);
        char dequeue();
        bool isEmpty() const;
        void swap(CharQueue2 & src);
        size_t capacity() const;
    private:
        std::deque<char> vardeque;
};

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

int main()
{
    //Create an object
    CharQueue2 q;
    
    //Next step is to call method enqueue and pass argument

    return 0;
}

Last edited on
For your stated problem: line 22 should be
void CharQueue2::enqueue (char ch)

Separate issue: line 10 could be
CharQueue2(){}
(although there are other ways round the problem).
I missed that one. Thank you.
If possible, can you confirm what is wrong with the way I am trying to call 'enqueue' and pass the argument

main.cpp:24:6: note: initializing argument 1 of ‘void CharQueue2::enqueue(char)’
void CharQueue2::enqueue (char ch)
^~~~~~~~~~

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
#include <deque>
#include <iostream>

class CharQueue2 
{
    public:
        CharQueue2();
        CharQueue2(size_t size);
        void enqueue(char ch);
        char dequeue();
        bool isEmpty() const;
        void swap(CharQueue2 & src);
        size_t capacity() const;
    private:
        std::deque<char> vardeque;
};

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

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

int main()
{
    //Create an object
    CharQueue2 q;
    //Next step is to call method enqueue and pass argument
    q.enqueue("a");

    return 0;
}
'a', not "a", on line 34.
True, since "a" is a std::string.
'a' is a type char.

THANK YOU!!!
Actually, it was a const char *, not a std::string.
Topic archived. No new replies allowed.