简体   繁体   中英

IO operator overloading error

In .h file

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

In .cpp file

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

Errors I am getting:

error C2143: syntax error : missing ';' before '&'
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

I've checked every IO overloading tutorial I can found yet I cannot fix this.

EDIT: adding std:: fixed every error except "> error C2805: binary 'operator <<' has too few parameters " I dont know what that means

EDIT2: declaring function as a friend solved this. thanks everyone!

You are using ostream without qualifying it with namespace std::

Use

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

I think you missed friend keyword.

And I recommend you official documentaion: http://en.cppreference.com/w/cpp/language/operators

And also you missed using namespace std; or std::ostream& .

I hope you this answer will help your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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