C++ STL/Syntax Question

Crono5Crono5 Join Date: 2003-07-22 Member: 18357Members
<div class="IPBDescription">Overloaded operator defined in a namesapce?</div>I am working on a project which uses lists, and I've overloaded the stream insertion operator (<<) for lists in a function with a header like so:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->template <typename T>
std::ostream& operator << (std::ostream& o, const std::list<T>& l)<!--c2--></div><!--ec2-->

The function works fine. However, I have use for it across multiple files, so I went to move it into a header. Then I realized there were a few other functions I could also group along with it, tentatively "miscellaneous functions I'm probably gonna use a lot", which I decided, in order to presumably improve the organization of my code, I would stick in a namespace, which I called Util.

So I have a Util.h which contains:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->namespace Util
{
    template <typename T>
    std::ostream& operator << (std::ostream&, const std::list<T>&);

    // ... other functions...
};<!--c2--></div><!--ec2-->

And, in Util.cpp, I have
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->template <typename T>
std::ostream& Util::operator << (std::ostream& o, const std::list<T>& l)
{
//...
}<!--c2--></div><!--ec2-->

I can't seem to quite get it to work right. When I include "Util.h", even if I'm 'using namespace Util', it says it cannot find a match for the operator (i.e. cout << myList; will give me back "couldn't find matching operator '<<'", or something similar). If anyone has any tips or advice, would you please share them with me?

Thank you for your time!
-Crono

Comments

  • ArmageddonArmageddon Join Date: 2005-01-07 Member: 33055Members
    Hello,

    I have taken C++ before but I have never gotten this far.

    A better place to ask this question would be on www.daniweb.com. That site has hardcore C++ (and others) programming professionals who have been working for 20-30 years in the industry so dun worry about getting the wrong answer <img src="style_emoticons/<#EMO_DIR#>/tounge.gif" style="vertical-align:middle" emoid=":p" border="0" alt="tounge.gif" />

    Good day,

    The guy who posts topics without a description <img src="style_emoticons/<#EMO_DIR#>/tounge.gif" style="vertical-align:middle" emoid=":p" border="0" alt="tounge.gif" />
  • illhillh Join Date: 2004-08-31 Member: 31104Members
    The two things that come to my mind in terms of what to possibly look for are:

    One -> to tinker around with your include order in your .cpp files

    Two -> tinker around with defining functions with inline so that you don't come across multiply defined function compile time errors when including said header in multiple .cpp files

    Also, you might want to discuss how and where you attempt to implement said header file in your actual code. Such as whether you included in the .h or .cpp and in what order and what not, just to be more concise.
Sign In or Register to comment.