In article <Jb******************************@garden.net>,
de********@hotmail.com says...
Hello,
I'm trying to create my own user define container, but I'm having a
little hard time figuring out why is my class considered undefined by my
compiler. Here is the following code.
[ ... ]
using namespace std;
As already noted, this is bad idea.
template< class T, class Allocator = allocator <T> >
class Pos_Neg_Array {
private:
This is redundant -- members of a class are private by
default.
[ ... ]
// constructor
Pos_Neg_Array()
};
You have to do one of two things: either declare your
ctor (which ends in a semicolon) or else define it (put a
body on the end). For most C++ compilers, you'll want to
make it a definition:
Pos_Neg_Array() {}
// implementation file pos_neg_array.cpp
#include "pos_neg_array.h"
template <class T, class Allocator = allocator <T> >
Pos_Neg_Array<T>::Pos_Neg_Array()
{
}
You want to put the default argument only on the class
definition, not on the definition of the member function
(s). If you want to put your ctor into a separate source
file, you'll need to export it:
// pos_neg_array.h
#include <memory>
export template<
class T,
class Allocator=std::allocator <T>
class Pos_Neg_Array {
T * ptr;
unsigned zero;
unsigned pos_extent;
unsigned neg_extent;
Allocator a;
public:
typedef T valuetype;
typedef T allocator_type;
typedef T & reference;
typedef const T & const_reference;
typedef int size_type;
typedef int difference_type;
typedef T * pointer;
typedef const T * const_pointer;
Pos_Neg_Array();
};
#endif
// pos_neg_array.cpp
#include "pos_neg_array.h"
template <class T, class Allocator>
Pos_Neg_Array<T, Allocator>::Pos_Neg_Array() {}
Then the important point: you'll only be able to compile
this code with a compiler that supports the export
keyword. Right now, that mostly means Comeau's compiler.
There are persistent rumors that the Intel compiler has
undocumented support as well. This sounds halfway
reasonable, since both of these compilers are built
around the EDG C++ front-end. OTOH, Comeau also uses a
pre-linker to help out by re-invoking the compiler as
needed to instantiate the exported parts of the template
over the correct types as needed. I'm not at all sure how
(or even if) Intel handles that.
In any case, I don't know of any other compiler that even
attempts to implement export -- thus the advice above
that for most compilers, you just want to make the ctor
an inline function in the header and be done with it.
Given that the body of the ctor is empty, you'd probably
prefer that it be an inline function anyway. OTOH,
presumably at some point you'd want to add a few things
like allocating storage there, at which point exporting
it might start to make sense (if you can afford to ignore
compilers that don't support it).
--
Later,
Jerry.
The universe is a figment of its own imagination.