Ansonku
一般會員
發表:13 回覆:15 積分:5 註冊:2002-10-23
發送簡訊給我
|
下面的碼所發生的錯誤,可是我不知道錯在哪....我只好把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
發送簡訊給我
|
引言:
下面的碼所發生的錯誤,可是我不知道錯在哪....我只好把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 --
------ -- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
|