Hello I have created a template
Below is the header file:
#pragma once
#include <iostream>
using namespace std;
template <class T>
class Airplane {
private:
T model;
int counter;
public:
Airplane(T model);
};
.cpp file:
#include "pch.h"
#include "Airplane.h"
#include <string>
template <class T>
Airplane<T>::Airplane(T model) {
if (&model != NULL)
this->model = model;
this->counter = 1;
}
Then created a set template that can accept any data type or my created template Airplane but the set must contain unique objects.
set header file:
#pragma once
#include <vector>
#include <iostream>
using namespace std;
template <class T>
class set {
private:
vector <T> setvector;
public:
set();
void insert(T obj);
};
set .cpp file:
#include "pch.h"
#include "set.h"
#include <iterator>
template <class T>
set<T>::set() {
class vector<T>::iterator vi = this->setvector.begin();
}
template <class T>
void set<T>::insert(T obj) {
if (this->setvector.empty()) {
this->setvector.push_back(obj);
}
else {
class vector<T>::iterator vi = this->setvector.begin();
bool flag = false;
while (vi != this->setvector.end()) {
if (*vi == obj) {
flag = true;
break;
}
vi++;
}
if (flag == false)
this->setvector.push_back(obj);
}
in the main method when i tried to use the set using int or doulbe it works perfectly, however when i try to instantiate a new set using my template "Airplane", VS throw Error
C3203 'Airplane': unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type.
I have tried to create a specialization for my template but still c++ will not accept it since it seems like it is a template. I have tried using template <template <class> class T>
in the set template but still did not work.