重载输出运算符的问题 [英] issues with overloading the output operator

查看:154
本文介绍了重载输出运算符的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重载我的输出操作符,但它给了我这些错误:


错误C2143:语法错误:在''&''之前缺少'';'' />

错误C2433:''ostream'':''朋友''不允许数据声明


错误C4430:缺少类型说明符 - 假设为int 。注意:C ++不支持default-int


错误C2061:语法错误:标识符''ostream''


错误C4430:缺少类型说明符 - 假设为int。注意:C ++不支持default-int


错误C2805:二进制''运算符<<''参数太少


这里我的头文件中的原型函数:

am trying to overload my output operator but it gives me these errors:

error C2143: syntax error : missing '';'' before ''&''

error C2433: ''ostream'' : ''friend'' not permitted on data declarations

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2061: syntax error : identifier ''ostream''

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2805: binary ''operator <<'' has too few parameters

here my prototype function in the header file:

展开 | 选择 | Wrap | 行号

推荐答案

尝试删除关键字<$ c $来自您的声明的c>朋友。


原因是你的功能只是一个功能;它不是任何类的方法。
Try dropping the keyword friend from your declaration.

The reason is that your function is just that, a function; it is not a method of any class.


感谢'的Oralloy但这不是我们如何重载输出流操作符!

如果你有任何其他建议请随意。

PS:这里的关键字朋友是强制性的!
thank''s Oralloy but this is not how we overload the output stream operator!
if you have any other suggestion please feel free.
PS: the keyword friend here is mandatory !


从你的帖子中,Oralloy是正确的,而且friend关键字是错误的。一个朋友只能出现在一个类声明中,你发布的内容不在一个类声明中。


然后,编译器出现问题& ;.这意味着当编译器到达&它遇到了麻烦。最有可能的是,ostream或MyPoint在使用之前尚未声明。检查yu是否包含了相应的标题。
From your posting, Oralloy is correct and the friend keyword is in error. A friend can only occur inside a class declaration and what you have posted is not inside a class declaration.

Then, the compiler has a problem with the &. That means that by the time the compiler reached an & it was in trouble. Most likely, either ostream or MyPoint has no been declared before being used. Check that yu have included the appropriate headers.


这篇关于重载输出运算符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆