随笔 - 7  文章 - 15  trackbacks - 0
<2006年6月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

常用链接

留言簿(2)

随笔档案(7)

相册

搜索

  •  

积分与排名

  • 积分 - 15451
  • 排名 - 939

最新评论

阅读排行榜

评论排行榜

以前写代码的时候就遇到VC++对友元支持得不太好的问题,同时也看过侯捷老师对gnu c++, VC++, BCB 三种编译器的比较,其中VC++对模板友元的支持就不是很好。
今天晚上写了一个比较简单的链表的模板类,其中头文件Chain.h原来的代码如下:

#include <iostream>
using namespace std;

#ifndef _CHAIN
#define _CHAIN

template<class T>
class ChainNode
{
 friend class Chain<T>;
private:
 T data;
 ChainNode<T> *link;
};

template<class T>
class Chain{
public:
 Chain()
 {
  first = 0;
 };
 ~Chain();
 bool IsEmpty() const {return first == 0;}
 int Length() const;
 bool Find(int k, T& x) const;
 int Search(const T& x) const;
 //Chain<T>& Delete(int k, T& x);
 Chain<T>& Insert(int k, const T& x);
 void Output(ostream& out = cout) const;
private:
 ChainNode<T> *first; // 指向第一个节点的指针
};

#endif  // _CHAIN

结果报错:
--------------------Configuration: Chain - Win32 Debug--------------------
Compiling...
Chain.cpp
g:\work plan\c++ code practice\chain\chain.h(17) : error C2059: syntax error : '<'
        g:\work plan\c++ code practice\chain\chain.h(21) : see reference to class template instantiation 'ChainNode<T>' being compiled
g:\work plan\c++ code practice\chain\chain.h(17) : error C2238: unexpected token(s) preceding ';'
        g:\work plan\c++ code practice\chain\chain.h(21) : see reference to class template instantiation 'ChainNode<T>' being compiled
g:\work plan\c++ code practice\chain\chain.h(40) : error C2989: 'Chain' : template class has already been defined as a non-template class
        g:\work plan\c++ code practice\chain\chain.h(17) : see declaration of 'Chain'
g:\work plan\c++ code practice\chain\chain.cpp(6) : error C2059: syntax error : '<'
g:\work plan\c++ code practice\chain\chain.cpp(6) : error C2588: '::~Chain' : illegal global destructor
g:\work plan\c++ code practice\chain\chain.cpp(6) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

Chain.obj - 6 error(s), 0 warning(s)

感觉从代码来看应该是没有问题的,如果哪个高手看出问题来了请一定告诉我啊,如果知道编译不通过的原因也请一定要告诉我啊。没办法,最后采用解决的办法就是修改ChainNode的定义了,定义为结构体:)
template<class T>
struct ChainNode
{
  T data;
  ChainNode<T> *link;
};

反正结构体中的数据成员都是public的,至于访问限制的实现就依靠迭代器来实现了,g++的STL中的树结点不也是结构体吗?:)

posted on 2006-06-17 23:39 Bourne 阅读(2593) 评论(4)  编辑 收藏 引用

FeedBack:
# re: 微软VC++对友元类和模板的支持真的这么差吗? 2006-06-18 15:01 mroske
// 在 ChainNode 之前声明 Chain 。
template<class T>
class Chain;

template<class T>
struct ChainNode
{
friend class Chain<T>; // class Chain<T> 在没有声明之前
// 不被发现。。。
// 因为 friend class Chain<T>
// 不是声明 Chain 是类模板。
// -- 以上个人之见。
private:
T data;
ChainNode<T> *link;
};

  回复  更多评论
  
# re: 微软VC++对友元类和模板的支持真的这么差吗? 2006-06-18 21:59 Corner
是啊!缺了template<class T> class Chain;的前向声明在template<class T> struct ChainNode前面.  回复  更多评论
  
# re: 微软VC++对友元类和模板的支持真的这么差吗? 2006-06-19 09:59 Bourne
试了一下,果然是这个错误,谢谢朋友的指点啊!  回复  更多评论
  
# re: 微软VC++对友元类和模板的支持真的这么差吗? 2008-03-11 02:12 WR

#include <iostream.h>

template <typename T>
class Chain;

template<class T>
class ChainNode
{
friend class Chain<T>;
private:
T data;
ChainNode<T> *link;
};

template<class T>
class Chain{
public:
Chain()
{
// first = 0;
}
~Chain();

bool IsEmpty() const {return first == 0;}
int Length() const;
bool Find(int k, T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k, T& x);
Chain<T>& Insert(int k, const T& x);
void Output(ostream& out = cout) const;
private:

ChainNode<T> *first; // 指向第一个节点的指针
};   回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理