Template/Operator Overload/Linker Error woes

Discussion in 'C++' started by amhoov@gmail.com, Apr 25, 2007.

  1. Guest

    Hi all,

    OS - MacOS X 10.4.9
    Compiler - gcc 4.0 (XCode)

    I'm *extremely* new to C++ (but have extensive experience with Java),
    so please go easy on me.

    I'm running into a very odd situation which I believe is related to
    template instantiation. I need to include a header file from a library
    what includes a template implementation (.cpp) file in which the ==
    operator is overloaded. If I include this header file, my application
    won't link and gives me the following three errors:

    /usr/bin/ld: Undefined symbols:
    int operator==<OpenMesh::FaceHandle>(OpenMesh::FaceHandle const&,
    OpenMesh::FaceHandle const&)
    int operator==<OpenMesh::VertexHandle>(OpenMesh::VertexHandle const&,
    OpenMesh::VertexHandle const&)
    int operator!=<OpenMesh::HalfedgeHandle>(OpenMesh::HalfedgeHandle
    const&, OpenMesh::HalfedgeHandle const&)

    The weird part is that the OpenMesh::FaceHandle, VertexHandle, etc.
    classes are data structures from a totally separate library. They are
    members of the myMesh object (which is also a specialization of a
    template) in the main.cpp program.

    It seems like for some reason, the linker is looking for an
    overloading of the == and != operators that returns int (as defined in
    the GA1DArray.h template). The source code for the header which causes
    the problem and the main file in which it's included are below. I
    apologize for how complicated this code is, but I'm using a couple
    different libraries, and it's hard to explain everything briefly.
    Lastly, one important bit of information: this code COMPILES AND LINKS
    FINE W/ MSVC++ 7.0

    Thanks
    Aaron
    ---------

    ---GAArray.h (Actual included header is ga.h which includes a header
    which includes the implementation of a ---template the extends
    GAArray, but GAArray has the overload of the operator in question, ==
    returning int)

    #ifndef _ga_arraytmpl_h_
    #define _ga_arraytmpl_h_

    template <class T>
    class GAArray {
    public:
    GAArray(unsigned int s) : sz(s), a(sz ? new T[sz] : 0)
    {for(unsigned int i=0; i<sz; i++) a = (T)0; }
    GAArray(const GAArray<T> & orig){sz=0; a=(T *)0; copy(orig);}
    GAArray<T> & operator=(const GAArray<T> & orig){copy(orig); return
    *this;}
    GAArray<T> & operator=(const T array []) // no err checks!
    {for(unsigned int i=0; i<sz; i++) a = *(array+i); return
    *this;}
    virtual ~GAArray(){delete [] a;}

    GAArray<T> * clone(){return new GAArray<T>(*this);}
    operator const T * () const {return a;}
    operator T * () {return a;}
    const T & operator[](unsigned int i) const {return a;}
    T & operator[](unsigned int i) {return a;}
    void copy(const GAArray<T> & orig){
    size(orig.sz);
    for(unsigned int i=0; i<sz; i++)
    a = orig.a;
    }
    void copy(const GAArray<T> & orig, unsigned int dest,
    unsigned int src, unsigned int length){
    for(unsigned int i=0; i<length; i++) a[dest+i] = orig.a[src+i];
    }
    void move(unsigned int dest, unsigned int src, unsigned int length){
    if(src > dest)
    for(unsigned int i=0; i<length; i++) a[dest+i] = a[src+i];
    else if(src < dest)
    for(unsigned int i=length-1; i!=0; i--) a[dest+i] = a[src+i];
    }
    void swap(unsigned int i, unsigned int j){T tmp=a[j]; a[j]=a;
    a=tmp;}
    int size() const {return sz;}
    int size(unsigned int n){
    if(n == sz) return sz;
    T * tmp = (n ? new T[n] : 0);
    for(int i=((n < sz) ? n-1 : sz-1); i>=0; i--) tmp = a;
    delete [] a;
    a = tmp;
    return sz=n;
    }
    int equal(const GAArray<T> & b,
    unsigned int dest, unsigned int src, unsigned int length) const {
    for(unsigned int i=0; i<length; i++)
    if(a[dest+i] != b.a[src+i]) return 0;
    return 1;
    }

    protected:
    unsigned int sz; // number of elements
    T * a; // the contents of the array
    };


    template <class T> int
    operator==(const GAArray<T> & a, const GAArray<T> & b){
    if(a.size() != b.size()) return 0;
    return a.equal(b,0,0,a.sz);
    }
    template <class T> int
    operator!=(const GAArray<T> & a, const GAArray<T> & b){
    if(a.size() != b.size()) return 1;
    return a.equal(b,0,0,a.sz) ? 0 : 1;
    }

    #endif

    ---------main.cpp------------
    #include "MeshObject.h"
    #include "ga/ga.h"

    Mesh myMesh;


    int main(int argc, char **argv)
    {
    OpenMesh::IO::read_mesh(myMesh, "simple.off");
    return 0;
    }
     
    , Apr 25, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > Hi all,
    >
    > OS - MacOS X 10.4.9
    > Compiler - gcc 4.0 (XCode)
    >
    > I'm *extremely* new to C++ (but have extensive experience with Java),
    > so please go easy on me.
    >
    > I'm running into a very odd situation which I believe is related to
    > template instantiation. I need to include a header file from a library
    > what includes a template implementation (.cpp) file in which the ==
    > operator is overloaded. If I include this header file, my application
    > won't link and gives me the following three errors:
    >
    > /usr/bin/ld: Undefined symbols:
    > int operator==<OpenMesh::FaceHandle>(OpenMesh::FaceHandle const&,
    > OpenMesh::FaceHandle const&)
    > int operator==<OpenMesh::VertexHandle>(OpenMesh::VertexHandle const&,
    > OpenMesh::VertexHandle const&)
    > int operator!=<OpenMesh::HalfedgeHandle>(OpenMesh::HalfedgeHandle
    > const&, OpenMesh::HalfedgeHandle const&)
    >

    Why do these return int rather than bool?

    > The weird part is that the OpenMesh::FaceHandle, VertexHandle, etc.
    > classes are data structures from a totally separate library. They are
    > members of the myMesh object (which is also a specialization of a
    > template) in the main.cpp program.
    >

    Your code doesn't define these operators, are you sure the library does?

    --
    Ian Collins.
     
    Ian Collins, Apr 25, 2007
    #2
    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. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  2. tuko
    Replies:
    5
    Views:
    606
    mlimber
    Sep 16, 2005
  3. Atlas
    Replies:
    13
    Views:
    505
    Atlas
    Oct 20, 2005
  4. Fei Liu
    Replies:
    5
    Views:
    355
    Maxim Yegorushkin
    Feb 25, 2006
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    260
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page