全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1396
推到 Plurk!
推到 Facebook!

Template 在 BCB中的錯誤,有沒有人遇過

尚未結案
Ansonku
一般會員


發表:13
回覆:15
積分:5
註冊:2002-10-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-02-28 21:39:35 IP:140.115.xxx.xxx 未訂閱
下面的碼所發生的錯誤,可是我不知道錯在哪....我只好把CODE全貼出來 [Linker Error] Unresolved external 'operator +(const Kvec&, const Kvec&)' referenced from C:\DOCUMENTS AND SETTINGS\ANSONKU\桌面\BCB\STL_V0\UNIT1.OBJ //=================================================================== //--------------------------------------------------------------------------- Unit1.cpp #pragma hdrstop #include #include "kvec.h" //--------------------------------------------------------------------------- #pragma argsused int main(int argc, char* argv[]) { Kvec a(3,2) ; Kvec b(3,1) ; a.cat(b) ; //a=b; a=a b ; a.show() ; cout << a.size() ; system("pause") ; return 0; } //--------------------------------------------------------------------kvec.h #ifndef ku_vec_h_ #define ku_vec_h_ #include #include #include #include #include #include using namespace std ; template class Kvec { public: vector v ; public: vector::iterator pos ; //建構子--解構子 Kvec() ; Kvec(int Size) {v.resize(Size) ;}; Kvec(int Size , T InitialValue) {v.resize(Size , InitialValue);}; Kvec(Kvec &vec) ; ~Kvec() {v.~vector();} ; //一般Function int size() {return v.size();}; void show() ; void resize(int Size) ; void resize(int Size , T initialValue) ; void allocate(int Size) ; void allocate(int Size , T initialValue) ; void zero() ; T sum() ; T multiply() ; T at(int i) ; void cat(const Kvec& vec) ; //Overloading Operator T& operator [](int i) ; void operator=(const Kvec& vec) ; friend Kvec operator (const Kvec& v1,const Kvec& v2) ; } ; #endif //-------------------------------------------------------------------- kvec.cpp #include "kvec.h" //#include "unit1.cpp" #pragma hdrstop //============================Basic Function==================================== template Kvec::Kvec(Kvec&vec){ v.clear() ; v(vec.v) ; } //------------------------------------------------------------------------------ template void Kvec::resize(int _size) { v.resize(_size) ; } //------------------------------------------------------------------------------ template void Kvec::resize(int _size,T _initial) { v.resize(_size,_initial) ; } //------------------------------------------------------------------------------ template void Kvec::show(){ //copy(v.begin(),v.end(),ostream_iterator(cout," ")) ; for(int i=0;i void Kvec::allocate(int _size){ v.clear() ; v.resize(_size) ; } //------------------------------------------------------------------------------ template void Kvec::allocate(int _size,T _initial){ v.clear() ; v.resize(_size,_initial) ; } //------------------------------------------------------------------------------ template void Kvec::zero(){ fill(v.begin(),v.end(),0) ; } //------------------------------------------------------------------------------ template T Kvec::sum(){ return accumulate(v.begin(),v.end(),0.0) ; } //------------------------------------------------------------------------------ template T Kvec::multiply() { T _multiply = 1 ; for(int i=0;i T Kvec::at(int _i){ return v.at(_i) ; } //------------------------------------------------------------------------------ template void Kvec::cat(const Kvec& _vec) { int _size = size() ; resize(_size _vec.size()) ; for(int i=0;i<_vec.size();i ) v.at(_size i) = _vec.at(i) ; } //=============================Overloading Operator============================= template T& Kvec::operator [] (int i) { if(i<0||i>size()) { cout << "Error" << endl ; system("pause") ; abort() ; } return v.at(i) ; } //operator[] address //------------------------------------------------------------------------------ template void Kvec::operator=(const Kvec& vec) { if(size()!=vec.size()) allocate(vec.size()) ; copy(vec.v.begin(),vec.v.end(),this->v.begin()) ; } //------------------------------------------------------------------------------ template Kvec operator (const Kvec& v1,const Kvec& v2) { //Kvec _result ; return &v2 ; } 不知道有沒有人知道為啥為這樣ㄉ 還是template對bcb有些時候不可以 我是使用bcb 6.0 謝謝大家
RaynorPao
版主


發表:139
回覆:3622
積分:7025
註冊:2002-08-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-02-28 23:33:55 IP:61.221.xxx.xxx 未訂閱
引言: 下面的碼所發生的錯誤,可是我不知道錯在哪....我只好把CODE全貼出來 [Linker Error] Unresolved external 'operator (const Kvec&, const Kvec&)' referenced from C:\DOCUMENTS AND SETTINGS\ANSONKU\桌面\BCB\STL_V0\UNIT1.OBJ 不知道有沒有人知道為啥為這樣ㄉ 還是template對bcb有些時候不可以 我是使用bcb 6.0 謝謝大家
Ansonku 你好: 改成這樣寫吧 < class="code"> int main(int argc, char* argv[]) { Kvec *a=new Kvec(3, 2); Kvec *b=new Kvec(3, 1); // 中間這部分要修改,而且你原本 template class 的程式碼也要修改 delete a; delete b; return 0; } -- Enjoy Researching & Developing --
------
-- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
系統時間:2024-05-05 7:21:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!