2010-04-08 44 views
1

這晚,我想不出有什麼不對我的語法。我也問過其他人,他們無法找到任何所以我來到這裏的一個朋友的忠告語法錯誤。C++語法問題

template <typename TT> 
bool PuzzleSolver<TT>::solve (const Clock &pz) { 

    possibConfigs_.push(pz.getInitial()); 
    vector< Configuration<TT> > next_; 

    //error is on next line 
    map< Configuration<TT> ,Configuration<TT> >::iterator found; 

    while (!possibConfigs_.empty() && possibConfigs_.front() != pz.getGoal()) { 
    Configuration<TT> cfg = possibConfigs_.front(); 
    possibConfigs_.pop(); 
    next_ = pz.getNext(cfg); 

    for (int i = 0; i < next_.size(); i++) { 
     found = seenConfigs_.find(next_[i]); 
     if (found != seenConfigs_.end()) { 
     possibConfigs_.push(next_[i]); 
     seenConfigs_.insert(make_pair(next_[i], cfg)); 
     } 
    } 
    } 
} 

出了什麼問題?

感謝您的任何幫助。

+3

您是否收到編譯器錯誤?其通常有用的信息,開始與... – KevenK 2010-04-08 02:52:40

+2

那麼,什麼是你的編譯錯誤? – vladr 2010-04-08 02:53:19

+1

什麼是錯誤訊息? – 2010-04-08 02:54:07

回答

11

如果我沒記錯,這個語法是不明確的:

map< Configuration<TT> ,Configuration<TT> >::iterator found; 

嘗試,而不是:

typename map< Configuration<TT> ,Configuration<TT> >::iterator found; 
+0

謝謝修復工作。 – Doug 2010-04-08 02:57:12

+2

http://pages.cs.wisc.edu/~driscoll/typename.html參考這裏獲取更多信息 – 2010-04-08 03:01:10

0

對於相關的名稱,你需要使用類型名關鍵字。依賴名稱是依賴於模板參數的名稱。

所以,你必須:

typename map< Configuration<TT> ,Configuration<TT> >::iterator found; 

這是必要的,因爲你的地圖的類型不知道,直到你的類(PuzzleSolver)被實例化,因爲它取決於模板參數TT。

0

這是需要使用的typename,以確定取決於模板類型參數指定類型的典型例子。你想用

typename map< Configuration<TT>, Configuration<TT> >::iterator found; 

基本上,編譯器無法知道map< Configuration<TT>, Configuration<TT> >::iterator是一個類型,而不是如一個成員變量,除非你告訴它。任何時候當你使用一個取決於模板參數的命名類型時,你必須使用typename(除了少數例外情況,例如在構造函數的初始化列表中)。