泛型模版類出現了LNK2019: 無法解析的外部符號錯誤,模版lnk2019

來源:互聯網
上載者:User

泛型模版類出現了LNK2019: 無法解析的外部符號錯誤,模版lnk2019

我們在寫程式的大部分時間都不會去考慮在已有的知識上出現的匱乏,前兩天用C++寫了一個順序表,結果卻不盡人意,這個或許是因為人生就是這樣,在你已知的知識當中出現的知識漏洞你會很難發現,這裡還是感謝一下syc大哥,每次在VC驛站上發文章,他都給我回複了,也就是syc讓我更加的喜歡VC驛站。

行了,談談出現的錯誤

這裡是我之前出錯的代碼

http://www.cctry.com/thread-256893-1-1.html

這裡我放上了我成功後的代碼

SeqList.h

#include<iostream>#include<stdio.h>//預設的順序表大小const int defaultsize= 10;//類template<typename DateType>class SeqList{public:SeqList(int size = defaultsize){//判斷對應的順序表長度if (size > 0){maxSize = size;//資料數組eml = new DateType[size];}}//解構函式~SeqList(){//申請的數組不為空白時進行釋放if (eml != NULL){delete []eml;eml = NULL;}}int getLength(){return curLength;}//插入bool insertElem(DateType e);//刪除bool delElem(int index);//尋找DateType findElem(int index);//更改bool changeElem(int index, DateType e);private:DateType *eml;int maxSize;int curLength;};//插入template<class DateType>bool SeqList<DateType>::insertElem(DateType e){if (curLength < maxSize){eml[curLength] = e;curLength++;return true;}std::cout << "insertElem function the Array is full" << std::endl;return false;}//刪除template<class DateType>bool SeqList<DateType>::delElem(int index){if (curLength > index && index >= 0){for (int i = index; i < curLength; i++){eml[i] = eml[i + 1];}curLength--;return true;}std::cout << "delElem function wrong index" << std::endl;return false;}//尋找template<class DateType>DateType SeqList<DateType>::findElem(int index){if (index < 0 && index >= curLength){std::cout << "findElem function wrong index" << std::endl;return NULL;}return eml[index];}//更改template<class DateType>bool SeqList<DateType>::changeElem(int index, DateType e){if (index >= 0 && index < curLength){eml[index] = e;return true;}std::cout << "changElem function wrong index" << std::endl;return false;}

測試體

#include "SeqList.h"#include <iostream>//success in day 09-01//模版類的聲明和實現必須寫在同一個標頭檔中,否則就無法找到類中對應的函數int main(){//使用泛型類,並調用建構函式SeqList<int> list(10);//插入值for (int i = 0; i < 10; i++){list.insertElem(i);}//顯示值for (int i = 0; i < list.getLength(); i++){std::cout << list.findElem(i) << std::endl;}//更改值list.changeElem(3, 5);list.delElem(4);//顯示值for (int i = 0; i < list.getLength(); i++){std::cout << list.findElem(i) << std::endl;}char a[10];std::cin >> a;}

這裡也就是說出現了一個小小的問題,就是模版類的定義和實現體一般要卸載同一個標頭檔中。

下面來源於網上:

在C++標準中沒有明確規定,模版類的定義和實現體必須放置到同一個檔案中,因為模版類是在先行編譯期間進行,如果寫在兩個檔案中,增加了編譯器的實現難度,所以大多數編譯器一般都將模版類的定義和實現放置在同一個檔案中。VS2005支援了模版類的分離編寫,但是他又檢測不出來很多其他的錯誤,這不得不說還是需要一個天才的大腦來解決這個問題

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.