在模板,在那里,为什么我必须把typename和template上依赖的名字呢?究竟什么是依赖名称?我有以下代码:
template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
// ...
template<typename U> struct inUnion {
// Q: where to add typename/template here?
typedef Tail::inUnion<U> dummy;
};
template< > struct inUnion<T> {
};
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
// ...
template<typename U> struct inUnion {
char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …Run Code Online (Sandbox Code Playgroud) 我的编码风格包括以下习语:
class Derived : public Base
{
public :
typedef Base super; // note that it could be hidden in
// protected/private section, instead
// Etc.
} ;
Run Code Online (Sandbox Code Playgroud)
这使我能够使用"super"作为Base的别名,例如,在构造函数中:
Derived(int i, int j)
: super(i), J(j)
{
}
Run Code Online (Sandbox Code Playgroud)
或者甚至在其重写版本中从基类调用方法时:
void Derived::foo()
{
super::foo() ;
// ... And then, do something else
}
Run Code Online (Sandbox Code Playgroud)
它甚至可以链接(我仍然可以找到它的用途):
class DerivedDerived : public Derived
{
public :
typedef Derived super; // note that it could be hidden in
// protected/private section, instead
// Etc.
} ; …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个继承自 Tree 类的二叉搜索树类,但编译器表示 Tree 类的数据成员未在 BST 类中继承。
template <class T>
class Tree {
protected:
class Node {
public:
T value;
Node * left;
Node * right;
};
Node * root;
public:
Tree() : root(NULL) { }
};
Run Code Online (Sandbox Code Playgroud)
template <class T>
class SearchTree : public Tree<T> {
public:
void foo();
};
template <class T>
void SearchTree<T>::foo() {
Node * node = NULL; //error- Unknown type name 'Node'
root = node; //error- Use of undeclared identifier 'root'
}
Run Code Online (Sandbox Code Playgroud)
我希望能够从基类“Tree”访问节点和根。为什么编译器说它们未声明且未知?