how to define const double in a templated class

P

PengYu.UT

Hi,

The following code does not work. Is anyway to make this work?

Thanks,
Peng

enum dirflags {
deg0,
deg45,
deg90,
deg135,
deg180,
deg_135,
deg_90,
deg_45,
N
};

template <dirflags I>
struct angle {
const double theta = I * M_PI / N;
};
 
A

Alf P. Steinbach

* (e-mail address removed):
The following code does not work. Is anyway to make this work?

Thanks,
Peng

enum dirflags {
deg0,
deg45,
deg90,
deg135,
deg180,
deg_135,
deg_90,
deg_45,
N
};

template <dirflags I>
struct angle {
const double theta = I * M_PI / N;
};

You can't define the value of a non-integral constant in the class
definition, nobody really know why.

But you can define the value separately:

template< dirflags I >
struct angle { static double const theta; }

template< dirflags I >
double angle<I>::theta = I* M_PI / N;

Note that M_PI is not a standard.

Cheers, & hth.,

- Alf
 

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

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top