Thread: Overloaded << operator

  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

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Thank you so much! That works.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Hate to bother you again, but is there any easy explanation for why it gives the error "undefined reference to 'Set::displaySet()'" when i declare that method as a const. It works when it isn't const.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I get this error when I forget to add the const in the implementation.
    Kurt

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Hmm.

    Code:
    void displaySet() const;
    Code:
    void Set::displaySet() const
    {
         cout << "{";
         for(int i=1;i<=myList.getLength();i++)
         {
               cout<<getValue(i);
               if(i!=myList.getLength())
                    cout<<",";
         }
         cout << "}";
    }
    I changed both to const. I don't have to do anything differently when I use it throughout my cpp file do I?

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Don't think so. What's the problem ?
    Kurt

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    I changed displaySet to const in both the .h and .cpp and now it gives me the error "undefined reference to 'Set::displaySet()'".

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Don't understand why. Have you rebuilt the whole project.
    Kurt

  14. #14
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    You really are my hero. It worked.

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Happens quite often. Most of the IDE's are not smart enough to check headers for dependancies.
    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