Thread: Overloaded << operator

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Overloaded << operator

    Quick question. If I have a method to overload the << operator:
    Header:
    Code:
    ostream& operator <<(ostream &os, const Set &s);
    In the cpp:
    Code:
    ostream& operator <<(ostream &os, const Set &s)
    {
           //print to a file
    }
    Is this the correct way to do this? I have checked around and it says it's the right way to do it but I am still getting errors.
    Last edited by DivineSlayer936; 04-07-2007 at 10:38 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yes it is the correct way. If you'd tell what errors you get, we might even be able to tell you why.
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    I get "In file included from Set.cpp"
    "expected constructor, destructor, or type conversion before '&' token"
    "expected `,' or `;' before '&' token"

    What do I have to include?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    could be <iostream> or the header that declares Set.
    Kurt

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    The program works fine just before I included the 2 code snippets from above. I had it printing with cout and I just wanted to get the overloading code right before I actually used it in my driver.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I still think it should be
    Code:
    #include <iostream>
    using std::ostream;
    in Set.h
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem using overloaded << operator
    By Zildjian in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2004, 01:09 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. overloaded << operator doesnt work
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2002, 04:20 AM