Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

error C2679: binary '>>' : no operator found

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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;
}

>Solution :

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];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading