I'm new here and I've been working my way through an independent study class at my high school and I've run into a problem with templates in namespaces.
The program I'm working on needs to get a set of objects of a certain type (hence the template). The set should contain a vector and hopefully add, delete, and print out the contents of the set.
Here is my code for the Header file
#ifndef SET_H
#define SET_H
#include "stdafx.h"
#include <vector>
using namespace std;
namespace Setthing
{
template<class T>
class Set
{
public:
Set(vector<T>& vect);
void addItem(vector<T>& vect, T item, bool same);
void removeItem(vector<T>& vect, int position);
int getSize(vector<T>& vect);
T* toArray(vector<T>& vect);
};
}
#endif
And here is the implementation file
#include "stdafx.h"
#include "Set.h"
#include <vector>
using namespace std;
namespace Setthing
{
template<typename T>
Set<T>::Set(vector<T>& vect)
{
vect = {};
}
template<typename T>
void Set<T>::addItem(vector<T>& vect, T item, bool same)
{
same = false;
for (int i = 0; i < vect.size(); i++)
{
if (item == vect[i])
same = true;
}
if (same != true)
{
vect.push_back(item);
}
}
template<typename T>
void Set<T>::removeItem(vector<T>& vect, int position)
{
vect.erase(position);
}
template<typename T>
int Set<T>::getSize(vector<T>& vect)
{
return vect.size();
}
template<typename T>
T* Set<T>::toArray(vector<T>& vect)
{
T arr[vect.size()];
for (int i = 0; i < vect.size(); i++)
{
arr[i] = vect[i];
}
return arr;
}
}
And finally here is the main function
#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <typeinfo>
#include "Set.h"
using namespace std;
using namespace Setthing;
/*
template<class T>
class set
{
public:
set();
void addItem(vector<T>& vect, T item, bool same);
void removeItem(vector<T>& vect, int position);
int getSize(vector<T>& vect);
T *toArray();
};
void addItem(vector<T>& vect, T item, bool same)
{
same = false;
for (int i = 0; i < vect.size(); i++)
{
if (item == vect[i])
same = true;
}
if (same != true)
{
vect.push_back(item);
}
}
void removeItem(vector<T>& vect, int position)
{
vect.erase(position);
}
int getSize(vector<T>& vect)
{
return vect.size();
}
T *toArray()
{
}*/
int wmain(int argc, wchar_t *argv[])
{
std::vector<int> list1;
std::vector<string> list2;
Set set1 = Set(list1);
Set set2 = Set(list2);
int obj1;
int add1;
string obj2;
string add2;
int* point1;
string* point2;
bool same = true;
cout << "Input an int for object 1: ";
cin >> obj1;
cout << "\nInput a string for object 2: ";
cin >> obj2;
if (typeid(obj1) == typeid(list1))
list1.push_back(obj1);
else
cout << "\nERROR! The types are not the same.";
if (typeid(obj2) == typeid(list2))
list2.push_back(obj2);
else
cout << "\nERROR! The types are not the same.";
cout << "\nEnter a value you want to add to list 1: ";
cin >> add1;
set1.addItem(list1, add1, same);
cout << "\nEnter a value you want to add to list 2: ";
cin >> add2;
set2.addItem(list2, add2, same);
cout << "\nRemoving the first object of each list...";
set1.removeItem(list1, 0);
set2.removeItem(list2, 0);
cout << "\nPutting lists to pointers...";
point1 = set1.toArray(list1);
point2 = set2.toArray(list2);
return 0;
}
The errors I'm getting are saying that the Set class is out of reach and the argument list can't be accessed. It also says that there is no default constructor for the class and that the functions can't work.