2016-08-13 61 views
-1

我想讓一個函數返回一個指向鏈表中最小單元的指針,其中Cell是一個結構體。該函數給我錯誤說該函數缺少一個類型說明符。任何幫助表示讚賞。C++:無法返回指向結構的指針

.h文件中

private: 
// TODO: Fill this in with the implementation of your doubly-linked list 
// priority queue. You can add any fields, types, or methods that you 
// wish. 
struct Cell { 
    string value; 
    Cell * next; 
    Cell * prev; 
}; 
int count; 
Cell * root; 
void clear(); 
Cell * getSmallestCell(); 

.cpp文件而不是

Cell* DoublyLinkedListPriorityQueue::getSmallestCell() 

Cell * DoublyLinkedListPriorityQueue::getSmallestCell() { 
Cell * smallest = root; 
for (Cell * i = root; i != NULL; i = i->next) { 
    if (i->value < smallest->value) { 
     smallest = i; 
    } 
} 
return smallest; 

}

+0

你不能雙重標記C和C++嗎?這是令人困惑的,以提供一個很好的答案。 – JVApen

+0

@Jpenpen C在這裏從來沒有問題。 –

+0

它在你編輯之前被加了標籤。 (請參閱http://stackoverflow.com/posts/38930881/revisions) – JVApen

回答

0

應該

DoublyLinkedListPriorityQueue::Cell* DoublyLinkedListPriorityQueue::getSmallestCell() 

或(因爲C++ 11)

auto DoublyLinkedListPriorityQueue::getSmallestCell() -> Cell * 

作爲Cell是內型的DoublyLinkedListPriorityQueue

+0

謝謝。如果我在其他函數內部使用它,我怎麼不需要指定Cell的範圍? –

+0

如果您在'DoublyLinkedListPriorityQueue'的成員方法中使用它,那麼您的範圍是正確的。在free函數裏,你必須使用'DoublyLinkedListPriorityQueue :: Cell'而不是'Cell'。 ('auto'的好用例縮短該類型的BTW)。 – Jarod42