读书人

模板类设为友元有关问题

发布时间: 2012-08-15 16:57:17 作者: rapoo

模板类设为友元问题?
假设
有A,B两个类.

在A.h文件中定义类A

#ifndef _A_H_
#define _A_H_

#include"B.h"

template< typename Object >
class A {
...
private :
class B m_b; A的成员中有一个B的对象
};

#endif

在A.cpp文件中实现了 class A


然后在B.h中定义类B
#ifndef _B_H_
#define _B_H_

#include"A.h"
template< typename Object >
class B {
friend class A<Object>; //将A设为友元. 但这里说不认识A.
...
};

#endif
在B.cpp中实现B.


求助,请问这是什么原因??


error:如下
e:\my\my_c++\ds_alg\set\binarytree.h(15) : error C2059: syntax error : '<'
e:\my\my_c++\ds_alg\set\binarytree.h(58) : see reference to class template instantiation 'BinaryTree<Object>' being compiled
e:\my\my_c++\ds_alg\set\binarytree.h(15) : error C2238: unexpected token(s) preceding ';'
e:\my\my_c++\ds_alg\set\binarytree.h(58) : see reference to class template instantiation 'BinaryTree<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(81) : error C2989: 'Set' : template class has already been defined as a non-template class
e:\my\my_c++\ds_alg\set\binarytree.h(15) : see declaration of 'Set'
E:\My\My_C++\DS_ALG\Set\Set.cpp(16) : error C2143: syntax error : missing ';' before '<'
E:\My\My_C++\DS_ALG\Set\Set.cpp(16) : error C2143: syntax error : missing ';' before '<'
E:\My\My_C++\DS_ALG\Set\Set.cpp(17) : error C2065: 'is' : undeclared identifier
E:\My\My_C++\DS_ALG\Set\Set.cpp(17) : error C2228: left of '.insert' must have class/struct/union type
BinaryTree.cpp
e:\my\my_c++\ds_alg\set\set.h(16) : error C2039: 'BinaryNode' : is not a member of '`global namespace''
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(16) : error C2629: unexpected 'class Set<Object>::const_iterator ('
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(16) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(36) : error C2143: syntax error : missing ';' before '<'
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(36) : error C2501: 'BinaryTree' : missing storage-class or type specifiers
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(36) : error C2059: syntax error : '<'
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(36) : error C2039: 'BinaryNode' : is not a member of '`global namespace''
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(36) : error C2238: unexpected token(s) preceding ';'
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(80) : error C2143: syntax error : missing ';' before '<'
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(80) : error C2501: 'BinaryTree' : missing storage-class or type specifiers
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled


e:\my\my_c++\ds_alg\set\set.h(80) : error C2059: syntax error : '<'
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
e:\my\my_c++\ds_alg\set\set.h(80) : error C2238: unexpected token(s) preceding ';'
e:\my\my_c++\ds_alg\set\set.h(81) : see reference to class template instantiation 'Set<Object>' being compiled
Error executing cl.exe.

Set.exe - 19 error(s), 0 warning(s)


[解决办法]
先google下模板分离编译的问题
[解决办法]
嵌套定义了。

[解决办法]
1. 把A中的class B m_b; 改为B* m_b;
2. 使用模板时,要注意目前很多编译器尚不支持分离编译。
[解决办法]
恩,记得以前msc就是不支持定义和实现分离的,不知道现在支持了没有~

读书人网 >C++

热点推荐