473,406 Members | 2,707 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,406 software developers and data experts.

recurring template problem

I've been reading (and using the examples in) Thinking in C++ (vol 2).

One of the things that the author shows you can do with templates is
the following:

#include <iostream>
using namespace std;

template<class T>
class Counted
{
static int count;
public:
Counted() { ++count; }
Counted(const Counted<T>&) { ++count; }
~Counted() { --count; }
static int getCount() { return count; }

};

template<class T>
int Counted<T>::count = 0;

// Curious class definitions
class CountedClass : public Counted<CountedClass> {};
class CountedClass2 : public Counted<CountedClass2> {};

int main()
{
CountedClass a;
cout << CountedClass::getCount() << endl; // 1
CountedClass b;
cout << CountedClass::getCount() << endl; // 2
CountedClass2 c;
cout << CountedClass2::getCount() << endl; // 1 (!)
} ///:~

I've tried adapting this idea to the following class that I've
used/created to try out some other things (Below is the almost working
example).

Header file:
[code]
#ifndef ID_CLASS_H
#define ID_CLASS_H

#include <map>

// magic number defines
#define NOT_A_VALID_ID -1

template<class T>
class ID_Class
{
private:
static std::map<int, int> ID_Map;
int ID_Value;

void DecrementReference();
void IncrementReference();

public:
// Only reason to ever NOT use the default constructor is when you
read
// the IDs from a source instead of creating new IDs.
ID_Class(int tID);
ID_Class();
~ID_Class();

ID_Class(const ID_Class<T>& Right);
ID_Class<T>& operator= (const ID_Class<T>& Right);

int Get_ID() const;

// Test/debug functions below this comment
void DumpMap() const;
};

#endif

cpp:
#include "ID_Class.h"

#include <stdexcept>
#include <ostream>
// note that #include <map> has been defined in the header. This is
needed
// for the static definition used there.

using std::cout;
using std::overflow_error;
using std::bad_alloc;
using std::pair;
using std::map;

template<class T>
map<int, int> ID_Class<T>::ID_Map;

// private functions
template<class T>
void ID_Class<T>::DecrementReference()
{
map<int,int>::iterator it = ID_Map.find(ID_Value);
--(it->second);
if ( it->second < 1)
{
ID_Map.erase(it);
}
}

template<class T>
void ID_Class<T>::IncrementReference()
{
map<int,int>::iterator it = ID_Map.find(ID_Value);
++(it->second);
if (it->second < 1)
{
throw overflow_error("To many copies exist");
}
}

// public functions
template<class T>
ID_Class<T>::ID_Class(int tID)
{
ID_Value = tID;
if ((ID_Map.insert(pair<int,int>(ID_Value,1))).second == false)
{
throw bad_alloc();
}
}

template<class T>
ID_Class<T>::ID_Class()
{
ID_Value = 1;
map<int,int>::const_iterator it (ID_Map.begin());
map<int,int>::const_iterator itEnd (ID_Map.end());

while(( it != itEnd) && (ID_Value == (*it).first))
{
++ID_Value;
++it;
}
if (ID_Value < 1)
{
throw overflow_error("Requested negative ID");
}
if ((ID_Map.insert(pair<int,int>(ID_Value,1))).second == false)
{
throw bad_alloc();
}
}

template<class T>
ID_Class<T>::~ID_Class()
{
DecrementReference();
}

template<class T>
ID_Class<T>::ID_Class(const ID_Class<T>& Right)
{
ID_Value = Right.ID_Value;
IncrementReference();
}

template<class T>
ID_Class<T>& ID_Class<T>::operator= (const ID_Class<T>& Right)
{
DecrementReference();
ID_Value = Right.ID_Value;
IncrementReference();
return *this;
}

template<class T>
int ID_Class<T>::Get_ID() const
{
return ID_Value;
}

// Test/Debug functions below this comment
template<class T>
void ID_Class<T>::DumpMap() const
{
map<int,int>::const_iterator it (ID_Map.begin());
map<int,int>::const_iterator itEnd (ID_Map.end());
while (it != itEnd)
{
cout << "ID " << (*it).first << " Has " << (*it).second << " copies"
<< endl;
++it;
}
}

main.cpp:

#include "ID_Class.h"
#include <iostream>

using namespace std
class MainID : public ID_Class<MainID>
{
};

int main( int argc, char* argv[])
{
// MainID TestID;

char t;
cin.get(t);
return(0);
};

I know that I'm missing something here since the code will compile
without a warning but when I uncomment // Main TestID; I get
unresolved externals (ctor & dtor). The question is what is is that am
I missing? What rules am I trying to break? And is there a way around
the problem?
Jul 22 '05 #1
4 1477

"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5**************************@posting.google.c om...
I've been reading (and using the examples in) Thinking in C++ (vol 2).

One of the things that the author shows you can do with templates is
the following:
[snip]
I know that I'm missing something here since the code will compile
without a warning but when I uncomment // Main TestID; I get
unresolved externals (ctor & dtor). The question is what is is that am
I missing? What rules am I trying to break? And is there a way around
the problem?


It's the old, old problem. Put all your template code in the header file,
that's where it belongs. I'm surprised Bruce Eckel didn't explain that in
his book, but here it is in the FAQ.

http://www.parashift.com/c++-faq-lit...templates.html

questions 34.12 to 34.15

john
Jul 22 '05 #2

"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5**************************@posting.google.c om...
I've been reading (and using the examples in) Thinking in C++ (vol 2).

One of the things that the author shows you can do with templates is
the following:
[snip]
I know that I'm missing something here since the code will compile
without a warning but when I uncomment // Main TestID; I get
unresolved externals (ctor & dtor). The question is what is is that am
I missing? What rules am I trying to break? And is there a way around
the problem?


It's the old, old problem. Put all your template code in the header file,
that's where it belongs. I'm surprised Bruce Eckel didn't explain that in
his book, but here it is in the FAQ.

http://www.parashift.com/c++-faq-lit...templates.html

questions 34.12 to 34.15

john
Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote in message news:<c4*************@ID-196037.news.uni-berlin.de>...
"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5**************************@posting.google.c om...
I've been reading (and using the examples in) Thinking in C++ (vol 2).

One of the things that the author shows you can do with templates is
the following:
[snip]

I know that I'm missing something here since the code will compile
without a warning but when I uncomment // Main TestID; I get
unresolved externals (ctor & dtor). The question is what is is that am
I missing? What rules am I trying to break? And is there a way around
the problem?


It's the old, old problem. Put all your template code in the header file,
that's where it belongs. I'm surprised Bruce Eckel didn't explain that in
his book, but here it is in the FAQ.


He did. In his first book, chapter 16. Guess this is what I get when I
just copy/paste existing files instead of trying to remember all the
excercises he'd suggested.

http://www.parashift.com/c++-faq-lit...templates.html

questions 34.12 to 34.15

Jul 22 '05 #4
"John Harrison" <jo*************@hotmail.com> wrote in message news:<c4*************@ID-196037.news.uni-berlin.de>...
"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5**************************@posting.google.c om...
I've been reading (and using the examples in) Thinking in C++ (vol 2).

One of the things that the author shows you can do with templates is
the following:
[snip]

I know that I'm missing something here since the code will compile
without a warning but when I uncomment // Main TestID; I get
unresolved externals (ctor & dtor). The question is what is is that am
I missing? What rules am I trying to break? And is there a way around
the problem?


It's the old, old problem. Put all your template code in the header file,
that's where it belongs. I'm surprised Bruce Eckel didn't explain that in
his book, but here it is in the FAQ.


He did. In his first book, chapter 16. Guess this is what I get when I
just copy/paste existing files instead of trying to remember all the
excercises he'd suggested.

http://www.parashift.com/c++-faq-lit...templates.html

questions 34.12 to 34.15

Jul 22 '05 #5

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

Similar topics

5
by: papi1976 | last post by:
Hello to everyone I heard about a strange thing about wich i have'nt find anything on the net or books, someone called this "Curiously recursive pattern" or a strange trick to play with...
14
by: John Harrison | last post by:
The following code does not compile template <class Derived> struct X { typedef typename Derived::type type; }; struct Y : public X<Y> {
4
by: velthuijsen | last post by:
I've been reading (and using the examples in) Thinking in C++ (vol 2). One of the things that the author shows you can do with templates is the following: #include <iostream> using namespace...
15
by: iuweriur | last post by:
A few questions on the curiously recurring template pattern: This page: http://c2.com/cgi/wiki?CuriouslyRecurringTemplate this part: template<typename T> struct ArithmeticType { T operator...
4
by: Martin MacRobert | last post by:
Hi Gang, The following code does not compile, but I can't figure out why. The compiler complains that the CuriouslyDerivedType (CRDerived) does not publish the "value_type", yet in fact the class...
1
by: steve | last post by:
Hi All I am writing a program for a gymnasium for membership control It is the first time I have had to deal with appointment diaries and I want to know the best way to store recurring...
4
by: chsalvia | last post by:
The "Curiously Recurring Template Pattern" (CRTP) recently caught my attention as a nice trick which allows static polymorphism. In other words, it lets you build extensible classes without the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.