After countless hours of google searching for answer, I wasn't able to find out how to override input operator for template class in c++. I have found answers for how to override operator for regular classes and how to override output operator. Trying to combine what I learnt from those led to nothing.
The code I wrote so far is:
Header file for class:
#include <iostream>
template <class T>
class Set
{
private:
T **array;
int max;
public:
Set();
Set(int);
~Set();
int Max()
{
return max;
};
friend std::ifstream& operator>> (std::ifstream &input, Set<T> &obj)
{
for (int i = 0; i < obj->max; i++)
{
T a;
input >> a;
obj.array[i] = new T;
obj.array[i] = a;
}
return input;
};
};
template <class T>
Set<T>::Set()
{
max = 0;
array = new T *[max];
}
template <class T>
Set<T>::Set(int a)
{
max = a;
array = new T *[max];
}
template <class T>
Set<T>::~Set()
{
delete[] array;
}
Main program:
#include "set.hpp"
int main(){
Set<float> obj;
std::cin >> obj;
return 0;
}
When trying to compile I get the following two errors:
no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'Set<float>')
no operator ">>" matches these operands -- operand types are: std::istream >> Set<float>
If it is important I am using gcc compiler. Thank you!