Re: Template and Overloading help

Discussion in 'C++' started by ES Kim, Jul 17, 2003.

  1. ES Kim

    ES Kim Guest

    "Todd Beauchemin" <> 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
     
    ES Kim, Jul 17, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:bf53ef$j5e$...
    > "Todd Beauchemin" <> 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
    >
    >
     
    Todd Beauchemin, Jul 17, 2003
    #2
    1. Advertising

  3. ES Kim

    ES Kim Guest

    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
     
    ES Kim, Jul 17, 2003
    #3
  4. "Todd Beauchemin" <> 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
     
    John Harrison, Jul 17, 2003
    #4
  5. "Todd Beauchemin" <> 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
     
    John Harrison, Jul 17, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,437
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    465
    Fredrik Lundh
    Sep 30, 2005
  3. Howard Gardner
    Replies:
    4
    Views:
    362
    Howard Gardner
    Jul 20, 2006
  4. Hicham Mouline
    Replies:
    0
    Views:
    450
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    436
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page