没有命名空间std的友元重载操作符
friend overloaded operator without namespace std
不知道有没有人能给我指出正确的方向。我有一个朋友提取操作符,如果我包括命名空间std;但如果我不这样做,就会失败。谁能给我点提示吗?
ostream& operator << (ostream &out, coins &value)
这也是一个好友函数,所以我在class.h文件中有这个函数(作为好友)在我的functions.h文件(作为原型)和functions.cpp文件(逻辑)中。
我试过了
std:: ostream&操作符……std:: ostream&运营商std:: & lt; & lt;(等)
但我就是看不出我错在哪里。我的编译器一直告诉我'ostream没有命名类型'
谢谢
存在于std
命名空间中的是ostream
,不要做std::<<
(这甚至没有意义!)尽量少用鸟枪式的方法来编程;也就是说,不要随便尝试,直到成功为止。错误告诉您ostream
(不合格)是问题所在,因此您必须首先解决该问题。
#include <iostream>
struct coins
{
friend std::ostream& operator<<(std::ostream& sink, const coins& value);
};
std::ostream& operator<<(std::ostream& sink, const coins& value)
{
sink << "doing coins output";
return sink;
}
int main()
{
coins c;
std::cout << c << std::endl;
}
顺便说一下,这是一个插入操作符,当你向流插入数据时。提取将是>>
相关文章:
- 运算符重载在 C++ 中使用友元函数
- C++模板内部类友元运算符重载
- C++ I/O 流重载:如何实现同一标头中相同 4 个 I/O 函数的 3 个不同类的友元重载?
- 不能在友元函数中使用重载运算符
- 使用shared_ptr向量重载 ostream 友元运算符
- 使用模板类重载的友元运算符
- 尝试重载模板类友元<<运算符时出错
- 使用友元函数重载新运算符
- C++重载友元函数无法访问私有成员
- 为什么重载运算符时需要友元关键字<<
- 我的运算符重载友元函数无法理解模板类型 T 的问题
- C++以友元身份重载 ostream 运算符会导致错误:变量在此上下文中是私有的
- 运算符重载函数作为友元
- 重载+运算符问题与其他类中的友元函数混合
- C++用参数重载友元类函数参数
- 将部分指定的模板化矩阵乘法运算符重载函数声明为类外的友元
- C++ <<运算符重载,无友元功能
- 使用模板实现友元重载<<运算符
- 没有命名空间std的友元重载操作符
- C++ 友元重载运算符<<