P
Prinse
Unregistered / Unconfirmed
GUEST, unregistred user!
各位:
下面的例子在编译时没有问题,可是链接时却报错了,不知何故?我用的是BCB6的BCC32编译和链接的。
#include <iostream.h>
template <class T>
class Data {
private:
T data;
public:
Data(T d=T()) : data(d) {};
friend Data operator+(const Data&, const Data&
friend ostream&
operator<<(ostream&, const Data&
};
template <class T>
Data<T> operator+(const Data<T>&
L, const Data<T>&
R) {
return Data<T>(L.data+R.data);
}
template <class T>
ostream&
operator<<(ostream&
os, const Data<T>&
D) { return os<<D.data;
}
void main() {
Data<int> a(1), b(2);
cout<<a<<endl;
cout<<b<<endl;
cout<<a+b<<endl;
}
下面是编译链接信息:
Borland C++ 5.6.1 for Win32 Copyright (c) 1993, 2002 Borland
D:/test.CPP:
Turbo Incremental Link 5.62 Copyright (c) 1997-2002 Borland
Error: Unresolved external 'operator +(const Data<int>&, const Data<int>&' referenced from D:/WORKS/TEMP/test.OBJ
Error: Unresolved external 'operator <<(_STL::basic_ostream<char, _STL::char_traits<char> >&, const Data<int>&' referenced from D:/WORKS/TEMP/test.OBJ
下面的例子在编译时没有问题,可是链接时却报错了,不知何故?我用的是BCB6的BCC32编译和链接的。
#include <iostream.h>
template <class T>
class Data {
private:
T data;
public:
Data(T d=T()) : data(d) {};
friend Data operator+(const Data&, const Data&
friend ostream&
operator<<(ostream&, const Data&
};
template <class T>
Data<T> operator+(const Data<T>&
L, const Data<T>&
R) {
return Data<T>(L.data+R.data);
}
template <class T>
ostream&
operator<<(ostream&
os, const Data<T>&
D) { return os<<D.data;
}
void main() {
Data<int> a(1), b(2);
cout<<a<<endl;
cout<<b<<endl;
cout<<a+b<<endl;
}
下面是编译链接信息:
Borland C++ 5.6.1 for Win32 Copyright (c) 1993, 2002 Borland
D:/test.CPP:
Turbo Incremental Link 5.62 Copyright (c) 1997-2002 Borland
Error: Unresolved external 'operator +(const Data<int>&, const Data<int>&' referenced from D:/WORKS/TEMP/test.OBJ
Error: Unresolved external 'operator <<(_STL::basic_ostream<char, _STL::char_traits<char> >&, const Data<int>&' referenced from D:/WORKS/TEMP/test.OBJ