Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Template and Overloading help

Reply
Thread Tools

Re: Template and Overloading help

 
 
ES Kim
Guest
Posts: n/a
 
      07-17-2003
"Todd Beauchemin" <(E-Mail Removed)> wrote in message
news:dGoRa.67887$GL4.18244@rwcrnsc53...
> Ok I am looking to make a templated function that will be able to output
> sets using the iostream overload <<.
>
> This is what I have
>
> in helper.hpp
>
> template<class T, class L, class A>
> ostream& operator << (ostream& o, set<T, L, A>& s);
>
> in helper.cpp
>
> template<class T, class L, class A>
> ostream& operator << (ostream & o, set<T, L, A>& s) {
>
> o << "SET {";
> for (set<T, L, A>::iterator itr = s.begin (); itr != s.end ();
> itr++) {
> if (itr != s.begin ()) o << ", ";
> o << (*itr);
> } // for
> o << "}";
>
> return o;
>
> } // operator <<
>
> However. I keep getting an undefined reference error when I link.
>
> I am using this in main
> which includes helper.hpp and is attempting to cout << myset << endl; where
> myset is set<int> myset;
>
> ~Todd
>
>


for (typename set<T, L, A>::iterator itr = s.begin (); itr != s.end ();

to give compilers a hint that set<T, L, A>::iterator is name of a type.

--
ES Kim


 
Reply With Quote
 
 
 
 
Todd Beauchemin
Guest
Posts: n/a
 
      07-17-2003

OK. that would explain why I get that warning when it compiles helper.cpp.
But im still getting the evil undefined error .

Thanks though
~Todd

"ES Kim" <(E-Mail Removed)> wrote in message
news:bf53ef$j5e$(E-Mail Removed)...
> "Todd Beauchemin" <(E-Mail Removed)> wrote in message
> news:dGoRa.67887$GL4.18244@rwcrnsc53...
> > Ok I am looking to make a templated function that will be able to output
> > sets using the iostream overload <<.
> >
> > This is what I have
> >
> > in helper.hpp
> >
> > template<class T, class L, class A>
> > ostream& operator << (ostream& o, set<T, L, A>& s);
> >
> > in helper.cpp
> >
> > template<class T, class L, class A>
> > ostream& operator << (ostream & o, set<T, L, A>& s) {
> >
> > o << "SET {";
> > for (set<T, L, A>::iterator itr = s.begin (); itr != s.end ();
> > itr++) {
> > if (itr != s.begin ()) o << ", ";
> > o << (*itr);
> > } // for
> > o << "}";
> >
> > return o;
> >
> > } // operator <<
> >
> > However. I keep getting an undefined reference error when I link.
> >
> > I am using this in main
> > which includes helper.hpp and is attempting to cout << myset << endl;

where
> > myset is set<int> myset;
> >
> > ~Todd
> >
> >

>
> for (typename set<T, L, A>::iterator itr = s.begin (); itr != s.end ();
>
> to give compilers a hint that set<T, L, A>::iterator is name of a type.
>
> --
> ES Kim
>
>



 
Reply With Quote
 
 
 
 
ES Kim
Guest
Posts: n/a
 
      07-17-2003
Well then, main() should know the definition of a template function,
not just a declaration. Did you #included helper.cpp in the file
containing main()?

--
ES Kim


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-17-2003

"Todd Beauchemin" <(E-Mail Removed)> wrote in message
news:nUoRa.67578$OZ2.12881@rwcrnsc54...
>
> OK. that would explain why I get that warning when it compiles

helper.cpp.
> But im still getting the evil undefined error .
>
> Thanks though
> ~Todd
>


All template code should go into header files. Throw away helper.cpp.

john


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-17-2003

"Todd Beauchemin" <(E-Mail Removed)> wrote in message
news:sewRa.82389$H17.25778@sccrnsc02...
>
> Yes. #include "helper.hpp"


Not helper.hpp, helper.cpp.

ES Kim is telling the same thing that I am, but in a different way. You
cannot put template code in source files.

Either you should put all the code in a header file (my suggestion)

Or you should include the source file in the header file (ES Kim's
suggestion).

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
overloading on the template parameter arity of a template templateparameter Howard Gardner C++ 4 07-19-2006 11:10 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments