+ 1

I am lost here, tried so much, [merge 2 array using operator overloading].

https://code.sololearn.com/cuHkW66lZwX9/?ref=app

26th Nov 2020, 2:25 AM
Bibek Pant
Bibek Pant - avatar
3 Respostas
+ 6
In the operator+ method of the Queue class, you are simply setting the value of each index of the new Queue instance, you are not updating its `size` attribute. So the `size` attribute will stay at 0, and because the print() method of your Queue class prints according to the `size` attribute, it will not print anything at all. In short, even though you have set the values in the new queue class, its `size` attribute is still 0 To fix the problem, either loop through the two queues separately and use the add() method that you have defined instead of setting the values of index. Example, instead of final.queue[i] = queue[i]; do final.add(queue[i]); This is the safer and preferred method, but you can also simply set the `size` attribute of the final queue to the `size of queue 1 + the size of queue 2`. Side note: Instead of using a concrete value as limit in the for loop on line 55, use `size` instead
26th Nov 2020, 2:58 AM
XXX
XXX - avatar
+ 5
not a good one; but a fix: #include <iostream> using namespace std; class Queue { int size; int queue[100]{}; public: Queue() { size = 0; } 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 +(const Queue& a) { Queue f; for(auto i= 0; i < this->size; i++) { f.add(this->queue[i]); } for(auto i= 0; i < a.size; i++) { f.add(a.queue[i]); } return f; } }; 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; } https://code.sololearn.com/cf8Nj0RAR75I/?ref=app
26th Nov 2020, 3:30 AM
Flash
0
Thank you so much both of you,
26th Nov 2020, 4:14 AM
Bibek Pant
Bibek Pant - avatar