How to.merge to.arrays.. what is the error is operator overload ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to.merge to.arrays.. what is the error is operator overload ?

#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; } Queue operator+(Queue &obj) { Queue res; int i,j; res.size = size + obj.size; for (i = size; i < size; i++) res.queue[i] = queue[i]; for (j = 0; j < obj.size; j++,i++) res.queue[i] = obj.queue[j]; return res; } }; int main() { Queue q1; q1.add(42); q1.add(2); Queue q2; q2.add(3); q2.add(56); Queue q3; q3 = q1+q2; q3.print(); }

22nd Jan 2021, 3:54 AM
Ayushi Mehta
Ayushi Mehta - avatar
1 Answer
+ 2
Please save your program in a C++ code and then link the code here so that it is easier to reference line numbers and compiler errors. Anyways, in the operator+() function, in the first for loop for (i = size; i < size; i++) why are doing `i = size`. The loop body will never be executed as i is never less than size, because you've set it *equal to* `size` Also, you're asking a question related to C++ and you've put the C# tag in your question. Please use suitable tags in your question. Kimdly change the c# to c++
22nd Jan 2021, 4:23 AM
XXX
XXX - avatar