2012-04-10 4 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 

これは私のコードをコンパイルしようとするときに受け取るものです。テンプレートクラスとその子クラスの関数呼び出しに基づいてエラーを受け取る

。これは、場合にはそれが必要だ、これですSortedListの、の子クラスである

#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 

#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 

を:私はここで、コードのこのセクション(私は信じている)にそれを絞り込むました助けてくれてありがとう。私たちの最後のクラスは継承のことは何も教えてくれませんでしたが、このクラスのプロジェクト#1であり、ここでも継承を教えているわけではないので、私はGoogleで調べることができたにもかかわらず、

+1

テンプレートの定義はヘッダファイルに行かなければなりません。このFAQを参照してください:[テンプレートクラスでリンカーエラーを避けるにはどうすればいいですか?](http://www.parashift.com/c++-faq/templates.html#faq-35.15) – ildjarn

+0

[テンプレートクラス - 未解決の外部シンボル:](http://stackoverflow.com/questions/5776862/template-class-unresolved-external-symbols) – ildjarn

+1

テンプレートリンカーの別のエラーdup:[なぜテンプレートはヘッダーファイルにのみ実装できますか?] http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)。 –

答えて

1

あなたのメソッドは定義されていません。リンカーは定義にリンクできないので不平を言っています。

+0

その定義はLinkedSortedList.cppにありますが、これはあなたの話ではありません。私もそれを提供することができますが、私はそれが誰かの喉を押しのけるコードの多くであると考えました。 –

+1

ああ、確か。だからあなたの質問のコメントのいくつかが示唆するように、あなたの問題は、同じクラスの外から呼び出している場合、テンプレートメソッドをヘッダファイルに定義する必要があるということです。したがって、実装をヘッダーファイルの一番下に移動すると、すべての設定が必要になります。 –

0

あなたの関数の定義をヘッダファイルに置いた方が役に立つかもしれません。これにより、コンパイラがこれらの外部シンボルを簡単に解決できるようになります。

こちらがお役に立てば幸いです。

よろしく、 Philinator