How to overload the friend extraction operator (>>) for templates in C++?

I am trying to overload the friend >> operator with templates. I don't want to define it inline.

I had tried to do the same with the help of the method add() defined in the code below. It works fine. I would like my >> operator to do the same.

The following is my code:

#include<iostream>

template<class T>class Demo;
template<class T>
std::ostream& operator<<(std::ostream&, const Demo<T> &);
template<class T>
std::istream& operator>>(std::istream&, const Demo<T> &);

template<class T>
class Demo {
private:
    T data; // To store the value.
public:
    Demo(); // Default Constructor.
    void add(T element); // To add a new element to the object.
    Demo<T> operator+(const Demo<T> foo);
    friend std::ostream& operator<< <T>(std::ostream &out, const Demo<T> &d);
    friend std::istream& operator>> <T>(std::istream &in, const Demo<T> &d);
};

template<class T>
Demo<T>::Demo() {
    data = 0;
}   

template<class T>
void Demo<T>::add(T element) {
    data = element;
}

template<class T>
Demo<T> Demo<T>::operator+(const Demo<T> foo) {
    Demo<T> returnObject;
    returnObject.data = this->data + foo.data;
    return returnObject;
}

template<class T>
std::ostream& operator<<(std::ostream &out, const Demo<T> &d) {
    out << d.data << std::endl;
    return out;
}

template<class T>
std::istream& operator>>(std::istream &in, const Demo<T> &d) {
    in >> d.data;
    return in;
}

int main() {
    Demo<int> objOne;
    std::cin>>objOne;
    Demo<int>objTwo;
    objTwo.add(3);
    Demo<int>objThree = objOne + objTwo;
    std::cout << "Result = " << objThree;
    return 0;
}

Actual Issue

While trying to overload the friend extraction operator ( >> ), the compiler shows the error as follows:

testMain.cpp:52:15:   required from here
testMain.cpp:46:8: error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'const int')
     in >> d.data;
        ^

Expected Output

Result = 59

RUN SUCCESSFUL (total time: 49ms)

References

  • "overloading the extraction operator >> in C++ [duplicate]" defines how to overload the >> operator. But it doesn't mention it's usage with templates.

  • "Overloading friend operator << for template class" defines how to overload the << operator, but not the >> .

  • "Overloading Extraction operator" also doesn't cover the template concept.


  • The problem has nothing to do with templates.

    operator>> modifies the data on the right-hand side, but you declared that parameter as const , so the operator cannot modify it. The compiler error even states that the value to be modified ( d.data ) is const:

    testMain.cpp:46:8: error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'const int' )

    You need to remove the const from the second parameter:

    template<class T>
    std::istream& operator>>(std::istream&, Demo<T> &);
    
    ...
    
    template<class T>
    class Demo {
       ...
    public:
        ...
        friend std::istream& operator>> <T>(std::istream &in, Demo<T> &d);
    };
    
    ...
    
    template<class T>
    std::istream& operator>>(std::istream &in, Demo<T> &d) {
        in >> d.data;
        return in;
    }
    
    链接地址: http://www.djcxy.com/p/73092.html

    上一篇: 运算符在Java中重载

    下一篇: 如何在C ++中为模板加载好友提取操作符(>>)?