2010-03-23 47 views
2

为什么GCC不允许默认参数?为什么C++不允许这个默认值?

template<class edgeDecor, class vertexDecor, bool dir> 
Graph<edgeDecor,int,dir> Graph<edgeDecor,vertexDecor,dir>::Dijkstra(vertex s, bool print = false) const 
{ 

这是输出我得到:

graph.h:82: error: default argument given for parameter 2 of ‘Graph<edgeDecor, int, dir> Graph<edgeDecor, vertexDecor, dir>::Dijkstra(Vertex<edgeDecor, vertexDecor, dir>, bool)’ 
graph.h:36: error: after previous specification in ‘Graph<edgeDecor, int, dir> Graph<edgeDecor, vertexDecor, dir>::Dijkstra(Vertex<edgeDecor, vertexDecor, dir>, bool)’ 

任何人都可以明白为什么我得到这个?

+0

我看不出为什么你得到这个,因为你只包括相关代码的一部分。你的错误同时涉及第36行和第82行,所以你需要在问题中包括这两个。 – 2010-03-23 00:35:59

+0

对不起布鲁克斯,好点。第36行是我班的函数原型。 – 2010-03-23 04:35:09

回答

8

您似乎已经在graph.h第36行中声明了函数(包括默认参数)。不要在func中重复默认值在宣言中指定一次就足够了。

2

您指定的模板参数之一:

Graph<edgeDecor,int,dir> Graph<edgeDecor,vertexDecor,dir>:: 
       ^^^ 

更改以匹配:

Graph<edgeDecor,vertexDecor,dir> Graph<edgeDecor,vertexDecor,dir>:: 
+0

在听到某人的建议后,这个错误消失了,这似乎不成问题。 – 2010-03-23 04:37:11

2

默认参数必须考虑只在你的方法,而不是定义

的声明
相关问题