473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

linking templates problem

I try to program a smart-pointer using policies. I rely heavyly on templates
during this. The program compiles fine - but I get a linker error:

test.o(.text+0x3e): In function `testSmartPtr()':
/home/tobias/project/Heart/heartofoak/test.cpp:23: undefined reference to
`SmartPtr<LongWrapper, RefCount>::SmartPtr[in-charge](LongWrapper*)'
collect2: ld returned 1 exit status

I think the linker does not find the Constructor of the
SmartPointer<LongWrapper, RefCount>.

The Code:

template <class T, template <class> class OwnershipPolicy>
class SmartPtr : public OwnershipPolicy<T> {
public:
explicit SmartPtr(T* pointer);
SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr& operator=(const SmartPtr<T, OwnershipPolicy>& otherSmartPtr);
~SmartPtr();
T& operator*() const;
T* operator->() const;

private:
void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr();
T* pointer_;

};

/* Policy Klassen fuer Kopien */
template <class T>
class RefCount {
private:
long *count_;
void initOwnership();
bool dispose();
bool copy(const SmartPtr<T, RefCount<T> >&);
friend class SmartPtr<T, RefCount>;
};

#include "smartptr.h"
#define DEBUG 1
#ifdef DEBUG
#include <iostream>
#endif

// not allowed, thus declared private and not implemented
template <class T, template <class> class OwnershipPolicy>
SmartPtr<T, OwnershipPolicy>::SmartPtr(){
}

template <class T, template <class> class OwnershipPolicy>
SmartPtr<T, OwnershipPolicy>::SmartPtr(T* pointer): pointer_(pointer) {
#ifdef DEBUG
std::cout << "SmartPtr Constructor\n";
#endif
initOwnership();
}

template <class T, template <class> class OwnershipPolicy>
SmartPtr<T, OwnershipPolicy>::~SmartPtr(){
#ifdef DEBUG
std::cout << "SmartPtr Destructor\n";
#endif
if (dispose() && pointer_!=NULL) {
delete pointer_;
}
}

template <class T, template <class> class OwnershipPolicy>
SmartPtr<T, OwnershipPolicy>::SmartPtr(const SmartPtr<T, OwnershipPolicy>&
otherPointer) {
#ifdef DEBUG
std::cout << "SmartPtr Copy-Constructor\n";
#endif
copyPtr(otherPointer);
}

template <class T, template <class> class OwnershipPolicy>
SmartPtr<T, OwnershipPolicy>& SmartPtr<T, OwnershipPolicy>::operator=(const
SmartPtr& otherSmartPtr) {
#ifdef DEBUG
std::cout << "SmartPtr Assignment Operator\n";
#endif
copyPtr(otherSmartPtr);
return *this;
}

template <class T, template <class> class OwnershipPolicy>
T& SmartPtr<T, OwnershipPolicy>::operator*() const {
return *pointer_;
}
template <class T, template <class> class OwnershipPolicy>
T* SmartPtr<T, OwnershipPolicy>::operator->() const {
return pointer_;
}

template <class T, template <class> class OwnershipPolicy>
void SmartPtr<T, OwnershipPolicy>::copyPtr(const SmartPtr<T,
OwnershipPolicy>& otherPointer) {
if (copy(otherPointer)) {
if (dispose() && pointer_!=NULL) {
delete pointer_;
}
}
pointer_=otherPointer.pointer_;
}

template <class T>
bool RefCount<T>::copy(const SmartPtr<T, RefCount<T> >& otherPointer) {
#ifdef DEBUG
std::cout << "RefCount::copy - count="<< (*count_) <<"\n";
#endif
count_=otherPointer.count_;
++(*count_);
return true;
}

template <class T>
void RefCount<T>::initOwnership() {
count_=new long(1);
#ifdef DEBUG
std::cout << "RefCount::initOwnership - count="<< (*count_) <<"\n";
#endif
}

template <class T>
bool RefCount<T>::dispose() {
--(*count_);
#ifdef DEBUG
std::cout << "RefCount::dispose - count="<< (*count_) <<"\n";
#endif
if (*count_==0) {
return true;
}
return false;
}

class LongWrapper {
private:
long x;
public:
LongWrapper(long l): x(l) {}

long get() { return x;}
};
bool testSmartPtr() {
LongWrapper *lp1=new LongWrapper(1);
/* HERE IS THE ERROR */
SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
RefCount>(lp1);
return true;
}

int main(int argc, char *argv[])
{
std::cout << "Hello, World!" << std::endl;

testSmartPtr();
return EXIT_SUCCESS;
}

Jul 19 '05 #1
1 2509
"Tobias Langner" <to************@t-online.de> wrote...
I try to program a smart-pointer using policies. I rely heavyly on templates during this. The program compiles fine - but I get a linker error:

test.o(.text+0x3e): In function `testSmartPtr()':
/home/tobias/project/Heart/heartofoak/test.cpp:23: undefined reference to

[...]

Place the template code in the header and next time start by
searching on http://groups.google.com. "template linker error
undefined reference" should have given you the answer way
before you could get this one.
Jul 19 '05 #2

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

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
1
by: murali | last post by:
Hi, I get tollowing linking errors Main.cpp:30 undefined reference to Matrix<int>::Matrix(int,int) whats that '' and what might be the problem ?? line 30 of main:...
2
by: Avi Uziel | last post by:
Hi All, I have a linkage problem that I believe related to template instantiation. My environment is Solaris 5.6, Compiler WorkShop 5. I'm building a shared library which use templates. During...
15
by: Rob Ratcliff | last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray X1. It makes heavy use of templates and namespaces. Up until the link step, the C++ source code compiled flawlessly. But, when...
3
by: hamishd | last post by:
TestClass.obj : error LNK2005: "int volatile bIsProcessing" (?bIsProcessing@@3HC) already defined in OtherClass.obj Creating library Release/TestDLL.lib and object Release/TestDLL.exp...
3
by: bav.272304 | last post by:
I have a code which uses external library with templates. The separate compiling of project files gets definition of the same symbols in different object files, so linking fails. Actually,...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
0
by: nachitas | last post by:
I was wondering if anybody could help me with this. I am new to CSS and think I understand it. My problem is that I had my template page working (it would update all my other pages) until I...
5
by: ciccio | last post by:
Hi, I have a problem with my code that the compiler does not find any inline functions which are static. The simple code example is written below, this is what the compiler throws at me. ]...
6
by: Joe.pHsiao | last post by:
Hi, I tried to link a C program to a library which is written by me in C+ +. I read some posts about linking a C program to C++ libraries. It seems doable by adding extern "C" to the C++ head...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.