- 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
19 Respostas
+ 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;
}
+ 5
Please check out how to properly declare a template class.
https://www.sololearn.com/learn/CPlusPlus/1916/?ref=app
+ 5
https://code.sololearn.com/cl9V8HA8hU3q/?ref=app
+ 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;
}
};
+ 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;
+ 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.
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;
0
}
0
Eror : expected '}' before size line 5
0
But i was creating template
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
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(
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;
}
0
Ok so it means in that queue only I have to create
0
Is this ok?
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;
}
0
Thank you
- 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
- 2