I am trying to create a generic class that handles ints, doubles, and strings. However, when trying to instantiate the template class with I get the following error message:
error: 'double' is not开发者_如何学C a valid type for a template constant parameter
The instantiation works completely fine with int types, as does the internal code, though I haven't made it to string types yet. It seems as if this should be fine, since you can instantiate vector, etc. Is there something I am missing here?
// file forest.h
template<typename NODETYPE> class Forest
{
template<NODETYPE> // Line 15
friend Forest<NODETYPE>& operator+(Forest<NODETYPE>& f1,
Forest<NODETYPE>& f2);
template<NODETYPE> // Line 17
friend ostream& operator<<(ostream& output,
const Forest<NODETYPE>& f1);
template<NODETYPE> // Line 19
friend void outputHelper(ostream& output,
const ForestNode<NODETYPE>& currentNode,
int depth);
/* ... */
};
The error occurs as follows:
\project 4\forest.h|341|instantiated from here|
\project 4\forest.h|15|error: 'double' is not a valid type for a template constant parameter|
\project 4\forest.h|17|error: 'double' is not a valid type for a template constant parameter|
\project 4\forest.h|19|error: 'double' is not a valid type for a template constant parameter|
template<NODETYPE> friend Forest<NODETYPE>& operator+(Forest<NODETYPE>& f1, Forest<NODETYPE>& f2);
template<NODETYPE> friend ostream& operator<<(ostream& output, const Forest<NODETYPE>& f1);
template<NODETYPE> friend void outputHelper(ostream& output, const ForestNode<NODETYPE>& currentNode, int depth);
These friend declarations are invalid. If you have a templated class, you don't need to repeat it's template arguments when referring to it within it's own scope. Even if you intended to allow any other instantiation of Forest, then you would have to use typename or class and call NODETYPE something else.
You can use double
(or float
or long double
) as a template parameter with any compiler that's even sort of close to conforming. What you can't do is use a floating point value as a non-type template parameter.
The closest you can get to this is generally passing the floating point value(s) to the ctor, and store it/them in your object.
you're most likely trying to do something like this:
template_type<3.1415926d> blarg;
somewhere, somehow.
this is not allowed. doubles (floats, long doubles) are not allowed as template constant parameters.
now something you probably will run into too is this:
template_type<"life, the universe and everything"> blarg;
this too is (for some reason) not admissable, since the pointer type should have external linkage so:
char* my_str="life, the universe and everything";
template_type<my_str> blarg;
shoule be just fine
now as a side note: some compilers do or did allow floating-point constants (iirc gcc 3, probably others)
Template constant parameter of floating point value (like double) are forbidden.
template <double x> struct A {};
However you can instantiate a template with type double
(what you'd like to do, if I get your question).
template <typename T> struct A {};
...
A<double> a;
If you want to specialize your template for the specific type double, then do
template <typename T> struct A {};
template <> struct A<double> {...};
精彩评论