2

I am trying to overload the << operator for my Currency class but I get this compiler error: C2143: syntax error : missing ';' before '&'

In my .h file I have:

 friend ostream &operator << (ostream &, const Currency&);

And in my Currency.cpp file I have:

    ostream &operator << (ostream &stream, const Currency &obj){
      stream<<"$"<<obj.dollars<<"."<<obj.cents;
      return stream;
      }

Everything up until now worked fine, but choked once I put that in:

I have the following at the top of my .h file:

    #ifndef CURRENCY_H
    #define CURRENCY_H

  #include<iostream>
  #include<string>
  #include<ostream>
  #include<sstream>

  class Currency; //forward delcaration

  //Function prototypes for overloaded stream operators
  ostream &operator << (ostream &, const Currency &);

I have no idea what I am doing wrong. Help would be great. Thanks

1
  • It pointed to the line where the functions were...it's resolved...it was a missing namespace. Commented Oct 16, 2011 at 4:00

1 Answer 1

10

ostream is declared in namespace std and you are missing std:: identifier before it:

std::ostream &operator << (std::ostream &, const Currency &);

If you want to avoid std:: then after header file you can put using namespace statement:

...
#include<ostream>
using namespace std;  // this is not desirable though in real world programming

ostream &operator << (ostream &, const Currency &);

Edit: using namespace <> is not recommended in real world programming at the file top. I have put that part just for FYI.

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, I just realized what I forgot in the head...you are correct but in the head I put using namespace std and that solved the issue like the example in the book. I'll accept this answer in 5 minutes
I upvoted you before seeing the part about adding the using statement, and now I wish I hadn't! Really? You're advocating adding using namespace std; to a header file at file scope??
@Praetorian, no I don't advocate it at all. Since this is homework, I stated that. I will edit that part.
@Praetorian you can remove the upvote. I am not sure about the disadvantages of doing using namespace std; as I am still in intro to CS and that is what our books/professor use.
@TravisPessetto I haven't removed the upvote because he did answer your question. The disadvantage of having using namespace std; in a header is that that declaration applies to every file that includes your header. So all translation units including it will have the contents of the std namespace visible in the global namespace, and since the std library defines functions with very commonly used names (std::find for instance) there are more chances of introducing name collisions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.