在模板,在那里,为什么我必须把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) 请考虑以下头文件:
template <typename T> struct tNode
{
T Data; //the data contained within this node
list<tNode<T>*> SubNodes; //a list of tNodes pointers under this tNode
tNode(const T& theData)
//PRE: theData is initialized
//POST: this->data == theData and this->SubNodes have an initial capacity
// equal to INIT_CAPACITY, it is set to the head of SubNodes
{
this->Data = theData;
SubNodes(INIT_CAPACITY); //INIT_CAPACITY is 10
}
};
Run Code Online (Sandbox Code Playgroud)
现在考虑来自另一个文件的一行代码:
list<tNode<T>*>::iterator it(); //iterate through the SubNodes
Run Code Online (Sandbox Code Playgroud)
编译器给我这个错误消息: Tree.h:38:17: error: need ‘typename’ before ‘std::list<tNode<T>*>::iterator’ because ‘std::list<tNode<T>*>’ …
我正在尝试编译C++的一些代码,这在Windows系统中可以正常运行.
我有很多错误,如下所示:
code:
..
39 set<Node<T>*>::iterator child;
...
Run Code Online (Sandbox Code Playgroud)
g++ 给我错误:
Node.h:39: error: expected ‘;’ before ‘child’
Run Code Online (Sandbox Code Playgroud)
这只是一个例子.你能给我一些如何解决它的提示吗?