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')
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?
+ 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}
?
+ 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;
}
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;
0
q3.print();
return 0;
}
0
This is my code
0
1st one
0
Hatsy Rei can you pls help
0
Thank you it's done
0
Can you suggest me that how I can start competitive programming?