0
votes

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!

2
where is object defined? - Patrick Sweigl
Sounds like ostream and object both need namespace qualification. The former would be std::ostream (make sure you have #include <ostream>) and the latter depends on where object is declared in your codebase. - cdhowie
If ostream is unknown, you probably missed including fstream or iostream. - Mats Petersson
@MatsPetersson Those will both include <ostream> but there is no reason to include them just to get std::ostream. - cdhowie

2 Answers

1
votes

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

Use

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

0
votes

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.