2010-10-31 116 views
3

我想创建一个处理整数,双精度和字符串的泛型类。然而,试图实例化模板类时,我得到了以下错误消息:双模板类型?

error: 'double' is not a valid type for a template constant parameter 

实例化运作为int类型完全没问题,一样的内部代码,虽然我还没有去到字符串类型尚未。看起来好像这应该没问题,因为你可以实例化向量等。有什么我在这里失踪?

// 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); 
    /* ... */ 
}; 

错误发生如下:

\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| 
+7

@joedillian:“有什么我在这里失踪?” - 是的。你在你的问题中缺少一些代码。如果您可以向我们展示一个能够再现问题的最小代码段,这将非常有帮助。 – 2010-10-31 21:51:17

+1

你想要有模板类型参数,有模板常量参数还是做模板特化?他们是不同的东西。也许你打算做他们其中的一个,并做了另一个意外?请显示一些代码,我们会帮助你弄清楚你正在做什么,以及你应该做什么。 – 2010-10-31 21:53:08

+0

说实话,我真的不知道如何制造这个错误的缩影。上次我发布时,我被告知我没有收到任何帮助,因为我的代码示例太大。现在我被告知我会被忽略,因为我无法提供代码。 : - /我会暂时发布我的全部代码。 – joedillian 2010-10-31 21:59:24

回答

4
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); 

这些朋友声明无效。如果你有一个模板类,当你在它自己的范围内引用它时,你不需要重复它的模板参数。即使你打算允许Forest的任何其他实例化,那么你将不得不使用typename或class并且调用NODETYPE别的东西。

+0

感谢您的帮助,我理解我的错误是什么,基本上我使用NODETYPE来指代森林的类型以及朋友函数中输入的类型,对吧? – joedillian 2010-10-31 22:58:10

+0

@ joedillian:是的。 – Puppy 2010-11-01 10:53:45

3

可以使用double(或floatlong double)与任何编译器,甚至有点接近于符合模板参数。你不能做的是使用浮点值作为非型模板参数。

最接近你的是通常将浮点值传递给ctor,并将它存储在你的对象中。

1

浮点值(如double)的模板常量参数被禁止。

template <double x> struct A {}; 

但是你可以实例化double类型的模板(你想要做的,如果我得到你的问题是什么)。

template <typename T> struct A {}; 
... 
A<double> a; 

如果你想专注您对特定类型的模板双,然后做

template <typename T> struct A {}; 
template <> struct A<double> {...}; 
+0

我的课程声明为模板,朋友功能函数声明为模板。然而,当我尝试实例化类 * foo =新类()它表示double不是可接受的参数。 – joedillian 2010-10-31 22:23:36

+1

好的,我明白了。错误信息可能会引起误解,但你不能做'template 朋友Forest &operator +(Forest &f1,Forest & f2);''就像'template fun(T t);'一个正确的原型看起来像'模板 fun(T t);' – log0 2010-10-31 22:38:14

2

你最有可能试图做这样的事情:

template_type<3.1415926d> blarg; 

的地方,不知何故。
这是不允许的。双打(浮动,长双打)不允许作为模板常量参数。 现在的东西,你可能会遇到过是这样的:

template_type<"life, the universe and everything"> blarg; 

这也是(由于某种原因)未admissable,由于指针类型应该有外部链接等等:

char* my_str="life, the universe and everything"; 
template_type<my_str> blarg; 

shoule只是细

现在作为一个侧面说明:一些编译器做还是没让浮点常量(IIRC的gcc 3,或许还有其它)