473,406 Members | 2,467 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.

static const members of template classes?

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

Jul 22 '05 #1
3 2568

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:Uc********************@speakeasy.net...
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
Jul 22 '05 #2
John Harrison wrote:
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

Jul 22 '05 #3

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:UN********************@speakeasy.net...
John Harrison wrote:
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.


What cases were those?

john
Jul 22 '05 #4

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

Similar topics

5
by: Thomas Matthews | last post by:
Hi, I have three classes: Category Author Publisher Each of these classes stores its information into a database table of <ID, text>. (They are fields that have a foreign key.) There is...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
1
by: Old Wolf | last post by:
I tried this code: #include <iostream> #include <string> template<typename T> struct enum_properties { static const long max; static const std::string name;
8
by: Jon Slaughter | last post by:
Is there any way to declare a member of a base class to make the itself in the derived class to be static? i.e., I have two classes class Base { protected: char *name;
5
by: shablool | last post by:
Hi all, Could someone please explain what is wrong with this code (the compiler complains about "'int_type' is not a member of 'B'"): struct B; template <typename DerivedT> struct A {
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.