473,480 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template/Operator Overload/Linker Error woes

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::FaceHan dle const&,
OpenMesh::FaceHandle const&)
int operator==<OpenMesh::VertexHandle>(OpenMesh::Verte xHandle const&,
OpenMesh::VertexHandle const&)
int operator!=<OpenMesh::HalfedgeHandle>(OpenMesh::Hal fedgeHandle
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[i] = (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[i] = *(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[i];}
T & operator[](unsigned int i) {return a[i];}
void copy(const GAArray<T& orig){
size(orig.sz);
for(unsigned int i=0; i<sz; i++)
a[i] = orig.a[i];
}
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[i];
a[i]=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[i] = a[i];
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 Tint
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 Tint
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 '07 #1
1 2742
am****@gmail.com 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::FaceHan dle const&,
OpenMesh::FaceHandle const&)
int operator==<OpenMesh::VertexHandle>(OpenMesh::Verte xHandle const&,
OpenMesh::VertexHandle const&)
int operator!=<OpenMesh::HalfedgeHandle>(OpenMesh::Hal fedgeHandle
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.
Apr 25 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2182
by: william xuuu | last post by:
Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ. ...
5
1767
by: tuko | last post by:
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 {
5
2743
by: Fei Liu | last post by:
Hi, I have a interesting problem here, class absOP{ template<class T> T operator(T val) { return val < 0 ? -val : val; } }; Now the problem is I can't seem to use this overloaded operator, ...
1
2110
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
6
1702
by: merdem | last post by:
Hi all, I just started to mess around with templates. First I declared a class Image as follows(this is a small version of the real thing which is pretty big): template<int depth, int space,...
3
3735
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
2520
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
2
1958
by: syang8 | last post by:
Dear all, I am trying to design classes with stream support. Basically, I want the operator << work for the base class and all the derived classes. If the base class is a template class, and...
5
2366
by: 2beagles | last post by:
I have a template for a three dimensional array that I have been working on using Visual C++ 6.0. Under version 6 this code worked fine. However, last night I downloaded Visual C++ 2005 Express and...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.