Hi guys I'm having trouble finding the bug in my code for project 6 C++ .CAN ANYONE PLEASE HELP ME | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

Hi guys I'm having trouble finding the bug in my code for project 6 C++ .CAN ANYONE PLEASE HELP ME

#include <iostream> using namespace std; class Queue{ int size; int x = 0; int *queue; public: Queue( ){ size = 0; queue = new int[100]; } Queue operator+(Queue &q1){ Queue q2; q2.queue = this -> queue + q1.queue; return q2; } void remove( ){ if(size == 0){ cout<<"Queue is empty"<<endl; } 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; } else{ for(int i = 0; i < size ; i++){ cout<<queue[i]<<" <- "; } cout<<endl; } } void add(int customerID){ queue[size]= customerID; size++; } }; 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 Jul 2021, 9:52 PM
Munashe Nyamukonda
Munashe Nyamukonda - avatar
2 Answers
+ 3
What is with the caps ? you don't add array or queue here like the following line, q2.queue=this -> queue + q1.queue. I would advise you to go through array section in c++ again!
25th Jul 2021, 10:00 PM
Abhay
Abhay - avatar
+ 2
I wanted to write a much longer, and brutal, answer. But what I'll say is: Use heap allocation only when you need it Use smart pointers Rule of 3: - Constructor - Copy-Constructor - Destructor If there's one, there must be all of them (like the Siths but with one more) Don't betray the heap, every new wants a delete (unless you use smart pointers, where it's hidden from you) Think of raw pointers as numbers operator+ and operator+= are lovers, they work better if they are together Good luck and have a nice day
26th Jul 2021, 12:15 AM
Angelo
Angelo - avatar