The full error message reads:
Error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Ar<int>' (or there is no acceptable conversion)
How can I fix it?
#include <iostream>
using namespace std;
template<class T>
class Dun
{
private:
T* array{ nullptr };
public:
Dun(T* _array) : array(_array) {}
T& Get(int index)
{
return this->array[index];
}
};
template<class T>
class Ar
{
private:
Dun<T>* data{nullptr};
public:
Ar(Dun<T>* _data) : data(_data) {}
T& operator[] (int index)
{
return this->data->Get(index);
}
};
int main()
{
int a[4] = { 1, 2, 3, 4 };
auto ar = new Dun<int>(a);
auto ur = new Ar<int>(ar);
cout << ur[1];
return 0;
}
CodePudding user response:
You don't need pointers here
int main()
{
int a[4] = { 1, 2, 3, 4 };
Dun<int> ar{a};
Ar<int> ur{&ar};
cout << ur[1];
return 0;
}
otherwise you'd have to dereference your pointer before your operator[]
can be used
cout << (*ur)[1];