- 1

It's time to update your Queue management system. The previous version supports only integer numbers and we need to support more

I wriiten a code but due to single eror my code is not compiling showing eror can anyone help me

6th Aug 2021, 10:18 AM
Rachita Bhasin
19 Answers
+ 8
#include <iostream> using namespace std; template <class T> class Queue { int size; T* queue; public: Queue() { size = 0; queue = new T[100]; } void add(T 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; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; }
14th Dec 2021, 7:54 AM
ANT T
ANT T - avatar
+ 5
Please check out how to properly declare a template class. https://www.sololearn.com/learn/CPlusPlus/1916/?ref=app
6th Aug 2021, 10:51 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
https://code.sololearn.com/cl9V8HA8hU3q/?ref=app
6th Aug 2021, 11:02 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
#include <iostream> using namespace std; template <class T> T { T size; T* 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; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } };
6th Aug 2021, 10:25 AM
Rachita Bhasin
+ 1
I mean, there's a whole bunch of stuff wrong here.. Declare the class with the class keyword. template <class T> class Queue { ... int main, not T main. The main function does not accept generic types. When instantiating Queue, use actual types. Queue<int> q1; ... Queue<std::string> q2;
6th Aug 2021, 10:42 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Rachita Bhasin You do not need two Queue classes. The whole premise of having templates is that you do not need to create multiple classes for different types. Remove Queue1.
6th Aug 2021, 10:53 AM
Hatsy Rei
Hatsy Rei - avatar
0
T1 { T1 size; T1* queue1; public: Queue() { size = 0; queue1 = new int[100]; } void add(String 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; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; T main() { Queue<T> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<T1> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0;
6th Aug 2021, 10:26 AM
Rachita Bhasin
0
}
6th Aug 2021, 10:26 AM
Rachita Bhasin
0
Eror : expected '}' before size line 5
6th Aug 2021, 10:27 AM
Rachita Bhasin
0
But i was creating template
6th Aug 2021, 10:42 AM
Rachita Bhasin
0
#include <iostream> using namespace std; template <class T> class Queue { T size; T* 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; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; class Queue1 { T size; T* queue; public: Queue1() { size = 0; queue= new int[100]; } void add(Strin
6th Aug 2021, 10:52 AM
Rachita Bhasin
0
class Queue1 { T size; T* queue; public: Queue1() { size = 0; queue= new int[100]; } void add(String 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; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue1<std::String> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(
6th Aug 2021, 10:52 AM
Rachita Bhasin
0
int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue1<std::String> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; }
6th Aug 2021, 10:53 AM
Rachita Bhasin
0
Ok so it means in that queue only I have to create
6th Aug 2021, 10:54 AM
Rachita Bhasin
0
Is this ok?
6th Aug 2021, 10:58 AM
Rachita Bhasin
0
int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<std::String> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; }
6th Aug 2021, 10:59 AM
Rachita Bhasin
0
Thank you
6th Aug 2021, 6:34 PM
Rachita Bhasin
- 1
#include <iostream> using namespace std; template <class T> class Queue { T size; T* queue; T* queue1; public: Queue() { size = 0; queue = new int[100]; queue1=new String[100]; } void add(int data) { queue[size] = data; size++; } void add(String 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; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<i
6th Aug 2021, 10:58 AM
Rachita Bhasin
6th Aug 2021, 11:00 AM
Rachita Bhasin