problem with template in extern .h and .cpp files

Discussion in 'C++' started by Stefan Bollmann, Nov 19, 2007.

  1. Hi,

    i wonder about a problem with functions i cannot put out of my
    main.cpp file into one *.h and *.cpp. When I run this little
    testprogram:

    #include <iostream>
    #include <vector>
    //#include"potential.h"
    using namespace std;

    template<class T>void test(T t);

    template<class T>void test(T t)
    {
    cout << t << endl;
    }

    int main ()
    {
    cout << "Il sole splende!" << endl;
    vector<vector<double> >A(2);
    for (int i=0;i<A.size();i++)
    A.resize(4,2);
    test((double)5.);

    return 0;
    }

    everything is well.
    If i compile the following code everythings fine at first, too, but
    running it produces a crash.
    Code:

    --- main.cpp ---

    #include <iostream>
    #include <vector>
    #include"potential.h"
    using namespace std;

    int main ()
    {
    cout << "Il sole splende!" << endl;
    vector<vector<double> >A(2);
    for (int i=0;i<A.size();i++)
    A.resize(4,2);
    test((double)5.);

    return 0;
    }

    --- potential.h ---

    #include <iostream>
    using namespace std;

    template<class T>void test(T t);

    --- potential.cpp ---

    #include "potential.h"

    template<class T>void test(T t)
    {
    cout << t << endl;
    }

    console:

    myPC>g++ main.cpp potential.h potential.cpp
    /usr/bin/ld: Undefined symbols:
    void test<double>(double)
    collect2: ld returned 1 exit status

    I wonder about it, because if i don't use templates everything is fine
    again:

    --- potential.h ---

    #include <iostream>
    using namespace std;

    void test(double t);

    -- potential.cpp ---

    #include "potential.h"

    void test(double t)
    {
    cout << t << endl;
    }

    and the main.cpp like the second example.
    What am I doing/thinking wrong?

    Thanks for reading ;-) ,
    Stefan
     
    Stefan Bollmann, Nov 19, 2007
    #1
    1. Advertising

  2. Stefan Bollmann

    Stefan Naewe Guest

    On 11/19/2007 2:38 PM, Stefan Bollmann wrote:
    > Hi,
    >
    > i wonder about a problem with functions i cannot put out of my
    > main.cpp file into one *.h and *.cpp. When I run this little
    > testprogram:
    >
    > #include <iostream>
    > #include <vector>
    > //#include"potential.h"
    > using namespace std;
    >
    > template<class T>void test(T t);
    >
    > template<class T>void test(T t)
    > {
    > cout << t << endl;
    > }
    >
    > int main ()
    > {
    > cout << "Il sole splende!" << endl;
    > vector<vector<double> >A(2);
    > for (int i=0;i<A.size();i++)
    > A.resize(4,2);
    > test((double)5.);
    >
    > return 0;
    > }
    >
    > everything is well.
    > If i compile the following code everythings fine at first, too, but
    > running it produces a crash.
    > Code:
    >
    > --- main.cpp ---
    >
    > #include <iostream>
    > #include <vector>
    > #include"potential.h"
    > using namespace std;
    >
    > int main ()
    > {
    > cout << "Il sole splende!" << endl;
    > vector<vector<double> >A(2);
    > for (int i=0;i<A.size();i++)
    > A.resize(4,2);
    > test((double)5.);
    >
    > return 0;
    > }
    >
    > --- potential.h ---
    >
    > #include <iostream>
    > using namespace std;
    >
    > template<class T>void test(T t);
    >
    > --- potential.cpp ---
    >
    > #include "potential.h"
    >
    > template<class T>void test(T t)
    > {
    > cout << t << endl;
    > }
    >
    > console:
    >
    > myPC>g++ main.cpp potential.h potential.cpp
    > /usr/bin/ld: Undefined symbols:
    > void test<double>(double)
    > collect2: ld returned 1 exit status
    >
    > I wonder about it, because if i don't use templates everything is fine
    > again:
    >
    > --- potential.h ---
    >
    > #include <iostream>
    > using namespace std;
    >
    > void test(double t);
    >
    > -- potential.cpp ---
    >
    > #include "potential.h"
    >
    > void test(double t)
    > {
    > cout << t << endl;
    > }
    >
    > and the main.cpp like the second example.
    > What am I doing/thinking wrong?
    >
    > Thanks for reading ;-) ,
    > Stefan


    Read this:
    http://www.ensta.fr/~diam/c /online/c -faq-lite/templates.html#faq-35.12

    S.
    --
    Stefan Naewe stefan dot naewe at atlas-elektronik dot com
    Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
    Plain text mails only, please http://www.expita.com/nomime.html
     
    Stefan Naewe, Nov 19, 2007
    #2
    1. Advertising

  3. Stefan Bollmann wrote:
    > i wonder about a problem with functions i cannot put out of my
    > main.cpp file into one *.h and *.cpp. When I run this little
    > testprogram:
    > [..]


    Please read the FAQ, the section on templates. Then look over
    the rest of the FAQ for link errors, and such. That should answer
    your concernes.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 19, 2007
    #3
  4. Ok, thx 2 all. The faq showed me the problem and now I know why I've
    got a problem. Sry for not looking there..

    Stefan
     
    Stefan Bollmann, Nov 19, 2007
    #4
  5. Shame wrote:
    > So, I'm experimenting with function objects (and Lambdas) and want to make a basic event/observer system. I created a class called Event, that takes a function definition as a template parameter (eg. void(int, int)) and then I allow observees to attach to the event
    > with the attach method, passing in a function object. The function objects are defined using boost::bind and boost::mem_fn. In the code below, there are two problems: (1) I cannot use overloaded functions in the event handler itself (hence they're called tock0,
    > tock1, tock2, etc.) and (2) I have to explicitly define the appropriate binding for the number of parameters. I've done this below using #define macros called Argument0 (no arguments), Argument1 etc.
    >
    > So my question is this: is it possible to code it in such a way as both (1) and (2) dissapear? The code below should compile and run in a standard vs2010 console project. I don't know about g++.
    >
    > Thanks.
    >
    > #include <vector>
    > #include <iostream>
    > #include <boost/bind.hpp>
    > #include <boost/function.hpp>
    >
    > using namespace std;
    >
    > template<class T>
    > class Event {
    >
    > public:
    >
    > void Attach(boost::function<T> f)
    > {
    > MyListeners.push_back(f);
    > }
    >
    > void operator()() const
    > {
    > for_each(MyListeners.begin(), MyListeners.end(), [](boost::function<T> i) { i(); } );
    > }
    >
    > template<class P>
    > void operator()(P p) const
    > {
    > for_each(MyListeners.begin(), MyListeners.end(), [p](boost::function<T> i) { i(p); } );
    > }
    >
    > template<class P, class Q>
    > void operator()(P p, Q q) const
    > {
    > for_each(MyListeners.begin(), MyListeners.end(), [p, q](boost::function<T> i) { i(p, q); } );
    > }
    >
    > template<class P, class Q, class R>
    > void operator()(P p, Q q, R r) const
    > {
    > for_each(MyListeners.begin(), MyListeners.end(), [p, q, r](boost::function<T> i) { i(p, q, r); } );
    > }
    >
    > template<class P, class Q, class R, class S>
    > void operator()(P p, Q q, R r, S s) const
    > {
    > for_each(MyListeners.begin(), MyListeners.end(), [p, q, r, s](boost::function<T> i) { i(p, q, r, s); } );
    > }


    Instead of all this, it is better to specialize class event for each
    number of arguments. Then you do not need macros later on. Something
    like this :

    template < typename SignalType, int N >
    class Event;

    here specialize for each N

    template<class T>
    class Event< T, 0 > {
    public:
    void Attach(boost::function<T> f)
    {
    MyListeners.push_back(f);
    }

    void operator()() const
    {
    //call all callbacks
    }

    private:

    std::vector<boost::function<T>> MyListeners;
    };
     
    Vladimir Jovic, Aug 16, 2010
    #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. DrUg13
    Replies:
    1
    Views:
    475
    DrUg13
    Feb 10, 2004
  2. Alex Vinokur
    Replies:
    7
    Views:
    409
    Greg Comeau
    Nov 15, 2004
  3. Vinu
    Replies:
    9
    Views:
    622
  4. www.hitechskill.com
    Replies:
    0
    Views:
    1,351
    www.hitechskill.com
    Apr 9, 2006
  5. Andre
    Replies:
    5
    Views:
    541
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page