简体   繁体   English

IO操作员重载错误

[英]IO operator overloading error

In .h file 在.h文件中

ostream& operator <<(ostream &os,const object &);

In .cpp file 在.cpp文件中

ostream& operator <<(ostream &os,const object &mono)
{
    os << mono.coef<<" *X^"<<mono.degree;
    return os;      
}

Errors I am getting: 我遇到的错误:

error C2143: syntax error : missing ';' 错误C2143:语法错误:缺少';' before '&' 在“&”之前
error C4430: missing type specifier - int assumed. 错误C4430:缺少类型说明符-假定为int。 Note: C++ does not support default-int 注意:C ++不支持default-int
error C2061: syntax error : identifier 'ostream' 错误C2061:语法错误:标识符'ostream'
error C4430: missing type specifier - int assumed. 错误C4430:缺少类型说明符-假定为int。 Note: C++ does not support default-int error C2805: binary 'operator <<' has too few parameters 注意:C ++不支持default-int错误C2805:二进制'运算符<<'参数太少

I've checked every IO overloading tutorial I can found yet I cannot fix this. 我检查了所有可以找到的IO重载教程,但无法解决。

EDIT: adding std:: fixed every error except "> error C2805: binary 'operator <<' has too few parameters " I dont know what that means 编辑:添加std ::修复了除“>错误C2805之外的所有错误:二进制'运算符<<'参数太少”我不知道那是什么意思

EDIT2: declaring function as a friend solved this. EDIT2:声明为好友功能可以解决此问题。 thanks everyone! 感谢大家!

You are using ostream without qualifying it with namespace std:: 您正在使用ostream而不使用名称空间std::

Use 采用

std::ostream& operator <<(std::ostream& os,const object& mono)

I think you missed friend keyword. 我认为您错过了friend关键字。

And I recommend you official documentaion: http://en.cppreference.com/w/cpp/language/operators 并且我建议您使用官方文档: http : //en.cppreference.com/w/cpp/language/operators

And also you missed using namespace std; 而且您还错过了using namespace std; or std::ostream& . std::ostream&

I hope you this answer will help your problem. 希望您的回答对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM