template<T> operator+. linker error

Discussion in 'C++' started by tuko, Sep 16, 2005.

  1. tuko

    tuko Guest

    The following snipet gives a linker error.
    I don't get it...

    template<class T> class tran {
    public:
    public:
    private:
    };

    template<class T> class matrix {
    public:
    matrix(int n);
    public:
    friend tran<T>& operator+(const matrix &m1, const matrix &m2);
    private:
    int n_;
    };

    template<class T>
    matrix<T>::matrix(int n) : n_(n) {}

    template<class T>
    tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
    tran<T> *to = new tran<T>;
    return *to;
    }

    int main () {
    matrix<double> mat1(10), mat2(10), mat3(10);
    //
    operator+<double>(mat1, mat2); // point 1
    mat1+mat2; // point 2
    //
    return 0;
    }

    In the code above...

    Point 1. Calling explicitly the operator+.
    It compiles fine.

    Point 2. Gives linking error.

    F:\DOCUME~1\alan\LOCALS~1\Temp/cc2Faaaa.o(.text+0x70):matrix.cpp:
    undefined reference to `operator+(matrix<double> const&, matrix<double> const&)'

    I'm doing something wrong. I don't know what.
    Any help would be greatly appreciated.

    --
    tuko.
    tuko, Sep 16, 2005
    #1
    1. Advertising

  2. tuko wrote:
    > The following snipet gives a linker error.
    > I don't get it...
    >
    > template<class T> class tran {
    > public:
    > public:
    > private:
    > };
    >
    > template<class T> class matrix {
    > public:
    > matrix(int n);
    > public:


    Ttry removing the following line, and then see if your linker error
    disappears. The following line does not do what you think it does.

    > friend tran<T>& operator+(const matrix &m1, const matrix &m2);


    > private:
    > int n_;
    > };
    >
    > template<class T>
    > matrix<T>::matrix(int n) : n_(n) {}
    >
    > template<class T>
    > tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
    > tran<T> *to = new tran<T>;
    > return *to;
    > }
    >
    > int main () {
    > matrix<double> mat1(10), mat2(10), mat3(10);
    > //
    > operator+<double>(mat1, mat2); // point 1
    > mat1+mat2; // point 2
    > //
    > return 0;
    > }
    >
    > In the code above...
    >
    > Point 1. Calling explicitly the operator+.
    > It compiles fine.
    >
    > Point 2. Gives linking error.



    Best regards,

    Tom
    Thomas Tutone, Sep 16, 2005
    #2
    1. Advertising

  3. > > template<class T>
    > > tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
    > > tran<T> *to = new tran<T>;
    > > return *to;


    P.S. - that's a memory leak.


    Best regards,

    Tom
    Thomas Tutone, Sep 16, 2005
    #3
  4. tuko

    mlimber Guest

    Thomas Tutone wrote:
    > > > template<class T>
    > > > tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
    > > > tran<T> *to = new tran<T>;
    > > > return *to;

    >
    > P.S. - that's a memory leak.


    Unless the user does something equally awkward:

    tran<double>& t = m1+m2;
    delete &t;

    In any case, it's a practice to avoid!

    Cheers! --M
    mlimber, Sep 16, 2005
    #4
  5. mlimber wrote:
    > Thomas Tutone wrote:
    > > > > template<class T>
    > > > > tran<T>& operator+(const matrix<T> &m1, const matrix<T> &m2) {
    > > > > tran<T> *to = new tran<T>;
    > > > > return *to;

    > >
    > > P.S. - that's a memory leak.

    >
    > Unless the user does something equally awkward:
    >
    > tran<double>& t = m1+m2;
    > delete &t;


    Which wouldn't work for temporaries:

    tran<double>& t = m1 + m2 + m1;
    delete &t;

    Now there's a memory leak even using your method.

    > In any case, it's a practice to avoid!


    Agreed.

    Best regards,

    Tom
    Thomas Tutone, Sep 16, 2005
    #5
  6. tuko

    mlimber Guest

    Thomas Tutone wrote:
    [snip]
    > Which wouldn't work for temporaries:
    >
    > tran<double>& t = m1 + m2 + m1;
    > delete &t;
    >
    > Now there's a memory leak even using your method.


    Good point. Let's all avoid doing what the OP did!

    M
    mlimber, Sep 16, 2005
    #6
    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. Master of Puppets
    Replies:
    4
    Views:
    5,471
    Master of Puppets
    Apr 9, 2005
  2. Replies:
    3
    Views:
    320
  3. Nita Rai
    Replies:
    1
    Views:
    441
    mlimber
    May 9, 2006
  4. Robbie Hatley
    Replies:
    0
    Views:
    457
    Robbie Hatley
    Jun 28, 2006
  5. Replies:
    1
    Views:
    498
    Ian Collins
    Apr 25, 2007
Loading...

Share This Page