2010-03-23 111 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

默認參數必須考慮只在你的方法,而不是定義

的聲明
相關問題