很简单的模板类中重载了操作符,就是链接不能通过.
#include "stdafx.h"
using namespace std;
//The Binary Tree
template < class Type > class BinaryTree
{
public:
BinaryTree() : m_root(NULL) {}
friend ostream& operator << (ostream & outStream, const BinaryTree< Type >& Tree );
private:
Type m_root;
};
template <class Type> ostream& operator << (ostream & outStream, const BinaryTree< Type >& Tree )
{
out<< "Preorder traversal of binary tree.\n";
return out;
}
int _tmain(int argc, _TCHAR* argv[])
{
BinaryTree<int> trTest;
cout<<trTest;
return 0;
}
以上是代码,我的环境 W2k + SP4 VC.Net 2003中文
建立了一个console Application
提示的链接错误:
TestStream error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class BinaryTree<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$BinaryTree@H@@@Z) ,该符号在函数 _main 中被引用
没有办法了,谁能给出一个模板编程的注意事项呢。我刚刚开始学习template.
问题点数:60、回复次数:12Top
1 楼goodboy1881(积木)(谁都别拦着我在水源升星)回复于 2004-09-03 16:04:58 得分 10
原因是因为链接器没有找到你的重载的函数实体,也就是重载的流输出模板根本就没有实例化,解决的方法就是把这个重载的流的声明和友员的声明放到一起
Top
2 楼fangrk(加把油,伙计!)回复于 2004-09-03 16:09:30 得分 1
不是吧!
#include <iostream>
using namespace std;
在文件前面添加试试看Top
3 楼Soundboy(鬼猫)回复于 2004-09-03 16:25:45 得分 0
fangrk(加把油,伙计!) ( ) :
不是那个原因,我本来就已经有 iostream 和 std 了
------------------------
goodboy1881(三井)(为了更好的BS JAVA而学习JAVA中) ( ) :
为什么呢,下面的现象不能解释了:
更有意思的是,我在这个类中重载了<< 和 >>可以 >>可以通过,而 <<有链接错误,匪夷所思!!
高人指点!!
#include <iostream>
using namespace std;
template <class Type> class dataList
{
private:
Type* Element;
int ArraySize;
public:
dataList( int Size = 10) : ArraySize(Size), Element ( new Type [Size] ){}
~dataList (){delete[] Element;}
friend istream& operator >> (istream& inStream, const dataList< Type > & inList );
friend ostream& operator << (ostream & outStream, const dataList< Type > & outList );
};
template <class Type> istream& operator >> (istream& inStream, dataList< Type >& inList )
{
//inStream>><<"Enter Array Current Size:\t";
return inStream;
}
template <class Type> ostream& operator << (ostream & outStream, const dataList< Type > & outList )
{
return outStream;
}
int _tmain(int argc, _TCHAR* argv[])
{
dataList<int> TestList(10);
cin>>TestList; //可以通过
cout<<TestList; //??链接错误
return 0;
}
Top
4 楼qwertasdfg123(不想工作)回复于 2004-09-03 16:52:49 得分 5
看看下面的帖子吧
http://community.csdn.net/Expert/topic/3252/3252575.xml?temp=.1690332Top
5 楼goodboy1881(积木)(谁都别拦着我在水源升星)回复于 2004-09-03 17:07:53 得分 10
#include<iostream>
#include<ostream>
using namespace std;
//The Binary Tree
template < class Type > class BinaryTree
{
public:
BinaryTree() : m_root(NULL) {}
friend ostream& operator << (ostream & out, const BinaryTree< Type >& Tree )
{
out<< "Preorder traversal of binary tree.\n";
return out;
}
private:
Type* m_root;
};
int main()
{
BinaryTree<int> trTest;
cout<<trTest;
return 0;
}
这是改好的程序~ 只要把实现放进去就可以了额啊。Top
6 楼Jinhao(辣子鸡丁·GAME就这样OVER了)回复于 2004-09-03 17:13:14 得分 5
//The Binary Tree
template < class Type > class BinaryTree
{
public:
BinaryTree() : m_root(NULL) {}
friend ostream& operator << <>(ostream & outStream, const BinaryTree< Type >& Tree );
private:
Type m_root;
};
//下面是个模板函数, 所以在类中的声明需要那个<>
template <class Type>
ostream& operator << (ostream & outStream, const BinaryTree< Type >& Tree )
{
out<< "Preorder traversal of binary tree.\n";
return out;
}
-----
http://community.csdn.net/Expert/topic/2844/2844336.xml?temp=.1679193Top
7 楼qwertasdfg123(不想工作)回复于 2004-09-03 17:33:31 得分 0
// Jinhao(辣子鸡丁)的代码没有声明友元函数。
template <class Type> class BinaryTree;
template <typename type>
ostream & operator<< (ostream &,const BinaryTree<type> &);
//.....Top
8 楼fantasydog(百里怜雪)回复于 2004-09-03 17:46:40 得分 5
模版类中声明的函数是函数,而不是模版,如:
template<class Type> Class1<Type>::Fun1(){}
这里Fun1是属于类Class1<Type>的函数。
而当你定义友元时:
template<class Type>
ostream & operator<< (ostream &,const BinaryTree<type> &);
这是一个模版,不是函数
operator << <Type>()才是一个函数
Top
9 楼qwertasdfg123(不想工作)回复于 2004-09-03 17:53:45 得分 0
TO:Jinhao(辣子鸡丁)
我以为需要前向声明,刚才用gcc试了一下,不需要!
不好意思!Top
10 楼fangrk(加把油,伙计!)回复于 2004-09-03 23:30:20 得分 24
#include <iostream>
using namespace std;
template<class T>
class Test
{
public:
Test(const T& t):value(t){}
template<class CharT,class CharTraits>
friend
basic_ostream<CharT,CharTraits>& operator<<(basic_ostream<CharT,CharTraits>&,const Test<T>&);
private:
T value;
};
template<class T,class CharT,class CharTraits>
basic_ostream<CharT,CharTraits>& operator<<(basic_ostream<CharT,CharTraits>& os,const Test<T>& obj)
{
os<<"the value is "<<obj.value;
return os;
}
int main()
{
Test<int> E(1234);
cout<<E;
}
/*.net 2003 ok
borland c++ builder 6
Error E2335 c.cpp 25: Overloaded 'operator ostream & << <char,char_traits<char>
>(ostream &,const Test<int> &)' ambiguous in this context in function main()--不知道哪里重载了
*/
#include <iostream>
using namespace std;
template<class T>
class Test
{
public:
Test(const T& t):value(t){}
template<class CharT,class CharTraits>
basic_ostream<CharT,CharTraits>& OutputTo(basic_ostream<CharT,CharTraits>& bo) const
{bo<<"value is "<<value;return bo;}
template<class CharT,class CharTraits>
basic_istream<CharT,CharTraits>& InputFrom(basic_istream<CharT,CharTraits>& bi)
{bi>>value;return bi;}
private:
T value;
};
template<class T,class CharT,class CharTraits>
basic_ostream<CharT,CharTraits>& operator<<(basic_ostream<CharT,CharTraits>& os,const Test<T>& obj)
{
return obj.OutputTo(os);
}
template<class T,class CharT,class CharTraits>
basic_istream<CharT,CharTraits>& operator>>(basic_istream<CharT,CharTraits>& is,Test<T>& obj)
{
return obj.InputFrom(is);
}
int main()
{
Test<int> E(1234);
cout<<E<<'\n';
cin>>E;
}
/*.net 2003 ok
borland c++ builder 6 ok
推荐这种方法
*/Top
11 楼Soundboy(鬼猫)回复于 2004-09-05 19:50:07 得分 0
谢谢楼上的,那么为什么用 ostream就不行呢,这个是因为.Net2003的局限吗Top
12 楼fangrk(加把油,伙计!)回复于 2004-09-06 11:14:43 得分 0
我也不是很清楚
各个编译器在实现上总归有些不一样。
我认为比较好的方法应该被广泛支持,《C++标准程序库》中的举例就采用了这种方法。Top




