2012-11-13 94 views
2

我正在返回指向一個類內聲明的結構的指針的代碼的一些問題。這是我到目前爲止的代碼:函數返回指針結構

SortedList.h

#ifndef SORTEDLIST_H 
#define SORTEDLIST_H 

class SortedList{ 

public: 

    SortedList(); 

... 

private: 

    struct Listnode {  

     Student *student; 

     Listnode *next; 

    }; 

    static Listnode *copyList (Listnode *L); 

}; 

#endif 

SortedList.cpp

#include "SortedList.h" 

... 

// Here is where the problem lies 

Listnode SortedList::*copyList(Listnode *L) 

{ 

    return 0; // for NULL 

} 

顯然,複製列表方法不會編譯。我正在使用Microsoft Visual Studio,並且編譯器告訴我「Listnode」未識別。當我嘗試編譯,這裏是whhat我得到:

1>------ Build started: Project: Program3, Configuration: Debug Win32 ------ 

1> SortedList.cpp 

sortedlist.cpp(159):錯誤C2657:「排序列表:: *」在聲明的開頭找到(你忘了指定類型?)

sortedlist.cpp(159):錯誤C4430:缺少類型說明符 - int假定。注意:C++不支持默認int

sortedlist.cpp(159):錯誤C2065:L':未聲明的標識符

sortedlist.cpp(159):錯誤C4430:缺少類型說明 - 假定爲int。注意:C++不支持default-int

sortedlist.cpp(159):致命錯誤C1903:無法從先前的錯誤中恢復;正在停止編譯

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

幫助將不勝感激......儘快

+0

你應該使用的格式是Markdown:http://daringfireball.net/projects/markdown/basics – jogojapan

回答

2

裏面cpp文件,該功能應該被定義爲:

SortedList::Listnode* SortedList::copyList(ListNode* L) 
{ 
    return 0; //For NULL 
} 

此外,struct Listnode應該被聲明爲public或在class SortedList之外。

+0

非常感謝! – GammaGuy

+1

請注意,在括號內部,您不需要'SortedList ::',因爲那時您處於'SortedList'函數內部,因此您處於其範圍內,這意味着'Listnode'在範圍內。 – Jarryd