0

What am I doing wrong with the overloading operator?

The result should be a new queue, where the elements of the first queue come first, followed by the second queue's elements. Given the Queue class, overload the + operator, so that the code in main works and successfully adds two queues. ///////////////my code #include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } Queue operator+(Queue &obj) { Queue res; res.size= this->size+obj.size; return res; } void add(int data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } }; int main() { Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print(); return 0; }

25th Nov 2021, 4:49 PM
Rubayet Kamal
Rubayet Kamal - avatar
3 odpowiedzi
+ 2
By the overloading operator you have added only new size, but forgoten to add the new elements of second queue.
25th Nov 2021, 5:52 PM
JaScript
JaScript - avatar
+ 1
JaScript so do I make another overloading operator?
25th Nov 2021, 6:33 PM
Rubayet Kamal
Rubayet Kamal - avatar
0
No Rubayet Kamal . All is in the same function, which will return the sum.
25th Nov 2021, 7:12 PM
JaScript
JaScript - avatar