2012-04-10 42 views
0
1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::getfirst(int &)" ([email protected][email protected]@@[email protected]) referenced in function _main 
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::clear(void)" ([email protected][email protected]@@UAEXXZ) referenced in function _main 
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::print(void)const " ([email protected][email protected]@@UBEXXZ) referenced in function _main 
1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::insert(int)" ([email protected][email protected]@@[email protected]) referenced in function _main 
1>main.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::find(int)const " ([email protected][email protected]@@[email protected]) 
1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall LinkedSortedList<int>::size(void)const " ([email protected][email protected]@@UBEHXZ) 
1>c:\users\chris\documents\visual studio 2010\Projects\lab0\Debug\lab0.exe : fatal error LNK1120: 6 unresolved externals 

這是我試圖編譯我的代碼時收到的。我已經把範圍縮小到(我相信)在這裏的這段代碼:接收基於模板類和它的子類的函數調用的錯誤

#ifndef _LinkedSortedListClass_ 
#define _LinkedSortedListClass_ 


    #include "LinkedNode.h" 
    #include "SortedList.h" 

    template <class Elm> 
    class LinkedSortedList: public SortedList<int> {  
    public: 

     void clear(); 

     bool insert(Elm newvalue); 

     bool getfirst(Elm &returnvalue); 

     void print() const; 

     bool find(Elm searchvalue) const; 

     int size() const; 

    private: 
      LinkedNode<Elm>* head; 
    }; 

    #endif 

這是孩子班上排序列表,這是如此,如果它的需要..

#ifndef _SortedListClass_ 
#define _SortedListClass_ 

template <class Elm> class SortedList { 
public: 

    // ------------------------------------------------------------------- 
    // Pure virtual functions -- you must implement each of the following 
    // functions in your implementation: 
    // ------------------------------------------------------------------- 

    // Clear the list. Free any dynamic storage. 
    virtual void clear() = 0;   

    // Insert a value into the list. Return true if successful, false 
    // if failure. 
    virtual bool insert(Elm newvalue) = 0; 

    // Get AND DELETE the first element of the list, placing it into the 
    // return variable "value". If the list is empty, return false, otherwise 
    // return true. 
    virtual bool getfirst(Elm &returnvalue) = 0; 

    // Print out the entire list to cout. Print an appropriate message 
    // if the list is empty. Note: the "const" keyword indicates that 
    // this function cannot change the contents of the list. 
    virtual void print() const = 0; 

    // Check to see if "value" is in the list. If it is found in the list, 
    // return true, otherwise return false. Like print(), this function is 
    // declared with the "const" keyword, and so cannot change the contents 
    // of the list. 
    virtual bool find(Elm searchvalue) const = 0; 

    // Return the number of items in the list 
    virtual int size() const = 0; 
}; 

#endif 

非常感謝你的幫助;我們最後一堂課沒有給我們任何繼承權,但這是這個班的第一個項目,沒有在這裏教授繼承,所以這些都是我的觸摸點,儘管我設法在Google上看到了這些。

+1

模板定義必須放在頭文件中。看到這個FAQ:[我怎樣才能避免與我的模板類的鏈接器錯誤?](http://www.parashift.com/c++-faq/templates.html#faq-35.15) – ildjarn 2012-04-10 01:50:52

+0

可能重複[Template class - 未解析的外部symbol(s)](http://stackoverflow.com/questions/5776862/template-class-unresolved-external-symbols) – ildjarn 2012-04-10 01:51:13

+1

另一個可能的模板鏈接器錯誤dup:[爲什麼模板只能在頭文件中實現?]( http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)。 – 2012-04-10 01:53:09

回答

1

您的方法未定義。所以鏈接器抱怨,因爲它不能鏈接到他們的定義。

+0

他們的定義是在LinkedSortedList.cpp中,除非那不是你在說什麼?我也可以提供這一點,但我認爲這是很多代碼來推倒別人的喉嚨。 – 2012-04-10 01:45:52

+1

哦,當然。因此,如您的問題的一些意見所示,您的問題是,如果模板方法是來自同一類外部的調用,則必須在頭文件中定義模板方法。因此,將您的實現移動到頭文件的底部,並且應該全部設置。 – 2012-04-10 02:30:38

0

如果您將函數的定義放在頭文件中,可能會有所幫助。這使編譯器更容易解析這些外部符號。

我希望這會有所幫助。

Regards, Philinator