0

Can anyone tell this why this error is comming in c++ while adding two queues using + operator

No match for 'operator+'(operand types are 'Queue ' and 'Queue')

28th Jul 2021, 3:15 AM
Rachita Bhasin
10 Réponses
+ 4
Well .. because there is no matching operator+ for Queue types. There is no overload of the + operator to add two queues together. For this to work, you have to define what the + operator needs to do when both operands are of type Queue. Can we see your code? Are you dealing with the STL std::queue, or are you defining your own Queue class?
28th Jul 2021, 3:24 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Rachita Bhasin Let's say your queues store the following values q1 : {42, 2, 8, 1} q2 : {3, 66, 128, 5} do you want q1 + q2 to return {42,2,8,1,3,66,128,5} or {42+3, 2+66, 8+128, 1+5} ?
28th Jul 2021, 7:39 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Try adding this to your class. Queue operator+(Queue obj) { Queue newObj; for (int i=0; i<this->size; i++) newObj.add(this->queue[i]); for (int i=0; i<obj.size; i++) newObj.add(obj.queue[i]); return newObj; }
28th Jul 2021, 1:43 PM
Hatsy Rei
Hatsy Rei - avatar
0
#include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } 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;
28th Jul 2021, 4:56 AM
Rachita Bhasin
0
q3.print(); return 0; }
28th Jul 2021, 4:57 AM
Rachita Bhasin
0
This is my code
28th Jul 2021, 4:58 AM
Rachita Bhasin
0
1st one
28th Jul 2021, 8:26 AM
Rachita Bhasin
0
Hatsy Rei can you pls help
28th Jul 2021, 1:35 PM
Rachita Bhasin
0
Thank you it's done
28th Jul 2021, 1:47 PM
Rachita Bhasin
0
Can you suggest me that how I can start competitive programming?
28th Jul 2021, 1:48 PM
Rachita Bhasin