如何在 C++ 中重载模板的友元提取运算符 (>>)?
How to overload the friend extraction operator (>>) for templates in C++?
我正在尝试使用模板重载 friend >>
运算符。我不想内联定义它。
我曾尝试借助下面代码中定义的方法 add()
来做同样的事情。它工作正常。我希望我的 >>
接线员也这样做。
以下是我的代码:
#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;
}
实际问题
在尝试重载友元提取运算符 (>>
) 时,编译器显示如下错误:
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;
^
预期输出
Result = 59
RUN SUCCESSFUL (total time: 49ms)
参考资料
"overloading the extraction operator >> in C++ [duplicate]" 定义如何重载 >>
运算符。但是它没有提到它与模板的用法。
"Overloading friend operator << for template class" 定义如何重载 <<
运算符,而不是 >>
.
"Overloading Extraction operator"也没有涵盖模板概念。
问题与模板无关。
operator>>
修改右边的数据,但是你把那个参数声明为const
,所以操作者不能修改它。编译器错误甚至指出要修改的值(d.data
)是 const:
testMain.cpp:46:8: error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'const int')
您需要从第二个参数中删除 const
:
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;
}
我正在尝试使用模板重载 friend >>
运算符。我不想内联定义它。
我曾尝试借助下面代码中定义的方法 add()
来做同样的事情。它工作正常。我希望我的 >>
接线员也这样做。
以下是我的代码:
#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;
}
实际问题
在尝试重载友元提取运算符 (>>
) 时,编译器显示如下错误:
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; ^
预期输出
Result = 59 RUN SUCCESSFUL (total time: 49ms)
参考资料
"overloading the extraction operator >> in C++ [duplicate]" 定义如何重载
>>
运算符。但是它没有提到它与模板的用法。"Overloading friend operator << for template class" 定义如何重载
<<
运算符,而不是>>
."Overloading Extraction operator"也没有涵盖模板概念。
问题与模板无关。
operator>>
修改右边的数据,但是你把那个参数声明为const
,所以操作者不能修改它。编译器错误甚至指出要修改的值(d.data
)是 const:
testMain.cpp:46:8: error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'const int')
您需要从第二个参数中删除 const
:
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;
}