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上看到了這些。
模板定義必須放在頭文件中。看到這個FAQ:[我怎樣才能避免與我的模板類的鏈接器錯誤?](http://www.parashift.com/c++-faq/templates.html#faq-35.15) – ildjarn 2012-04-10 01:50:52
可能重複[Template class - 未解析的外部symbol(s)](http://stackoverflow.com/questions/5776862/template-class-unresolved-external-symbols) – ildjarn 2012-04-10 01:51:13
另一個可能的模板鏈接器錯誤dup:[爲什麼模板只能在頭文件中實現?]( http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)。 – 2012-04-10 01:53:09