static const members of template classes?

  • Thread starter Steven T. Hatton
  • Start date

S

Steven T. Hatton

Basically the question is, can I do this?:

template <typename T>
class Vector3 {
public:
Vector3(const T& x=0,
const T& y=0,
const T& z=0)
: _v(3)
{
_v[0]=x;
_v[1]=y;
_v[2]=z;
}

~Vector3()
{}

/*...*/

/** {1, 0 ,0} */
static const Vector3<T> I;
/** {0, 1 ,0} */
static const Vector3<T> J;
/** {0, 0 ,1} */
static const Vector3<T> K;


private:
vector<T> _v;
};

It works for non-template classes of the same form, but I keep getting
linker errors saying 'undefined reference to Vector3<float>I', etc.

This is what I currently have in the source file. I have no good reason to
believe it makes the least bit of sense, other than the fact the compiler
doesn't complain:

template<typename T>
const Vector3<T> Vector3<T>::I = Vector3<T>(1,0,0);

template<typename T>
const Vector3<T> Vector3<T>::J = Vector3<T>(0,1,0);

template<typename T>
const Vector3<T> Vector3<T>::K = Vector3<T>(0,0,1);

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
 
Ad

Advertisements

J

John Harrison

Steven T. Hatton said:
Basically the question is, can I do this?:

template <typename T>
class Vector3 {
public:
Vector3(const T& x=0,
const T& y=0,
const T& z=0)
: _v(3)
{
_v[0]=x;
_v[1]=y;
_v[2]=z;
}

~Vector3()
{}

/*...*/

/** {1, 0 ,0} */
static const Vector3<T> I;
/** {0, 1 ,0} */
static const Vector3<T> J;
/** {0, 0 ,1} */
static const Vector3<T> K;


private:
vector<T> _v;
};

It works for non-template classes of the same form, but I keep getting
linker errors saying 'undefined reference to Vector3<float>I', etc.

This is what I currently have in the source file. I have no good reason to
believe it makes the least bit of sense, other than the fact the compiler
doesn't complain:

template<typename T>
const Vector3<T> Vector3<T>::I = Vector3<T>(1,0,0);

template<typename T>
const Vector3<T> Vector3<T>::J = Vector3<T>(0,1,0);

template<typename T>
const Vector3<T> Vector3<T>::K = Vector3<T>(0,0,1);

Like all other template code this should go in the header file. Then you
won't get any linker errors.

john
 
S

Steven T. Hatton

John said:
Like all other template code this should go in the header file. Then you
won't get any linker errors.

john

I've come across that suggestion in the past, and used to accept it as hard
and fast until I found some cases where I was forced to put template
definitions in source files. Ever since then I have tried to treat the
template code like standard code as much as possible.

It worked to move it to the header this time. Thanks! I'll remember to try
that in the future.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
 
Ad

Advertisements

J

John Harrison

Steven T. Hatton said:
I've come across that suggestion in the past, and used to accept it as hard
and fast until I found some cases where I was forced to put template
definitions in source files.

What cases were those?

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top