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で調べることができたにもかかわらず、
テンプレートの定義はヘッダファイルに行かなければなりません。このFAQを参照してください:[テンプレートクラスでリンカーエラーを避けるにはどうすればいいですか?](http://www.parashift.com/c++-faq/templates.html#faq-35.15) – ildjarn
[テンプレートクラス - 未解決の外部シンボル:](http://stackoverflow.com/questions/5776862/template-class-unresolved-external-symbols) – ildjarn
テンプレートリンカーの別のエラーdup:[なぜテンプレートはヘッダーファイルにのみ実装できますか?] http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)。 –