[英]How to call constructor of a class that is within another class in C++?
Here is my code: 这是我的代码:
template<class Datatype>
class Node
{
public:
Node()
{
next = NULL;
prev = NULL;
}
Node* getNext()
{
return next;
}
Node* getPrev()
{
return prev;
}
Datatype* getData()
{
return &data;
}
void changeNext()
{
next = NULL;
}
void changeNext(Node& nextNode)
{
next = &nextNode;
}
void changePrev()
{
prev = NULL;
}
void changePrev(Node& prevNode)
{
prev = &prevNode;
}
Node* addNext(Node &);
Node* addPrev(Node &);
void nodeDel();
void addData(Datatype &);
private:
Node* next;
Node* prev;
Datatype data;
};
template<class Datatype>
class Stack
{
public:
Stack()
{
node.Node();
}
int push(Datatype &);
Datatype pop();
Datatype* peek();
private:
Node<Datatype> node;
};
The compiling error is like this: 编译错误是这样的:
my_node.h: In constructor ‘Stack<Datatype>::Stack() [with Datatype = float]’:
test.cpp:8:15: instantiated from here
my_node.h:58:4: error: invalid use of ‘Node<float>::Node’
At beginning, I didn't wrote the constructor for Stack. 一开始,我没有为Stack编写构造函数。 However, I felt that actually the constructor of node defined by me was not been called, because there are some values in next and prev pointers.
但是,我觉得实际上没有调用我定义的node的构造函数,因为next和prev指针中有一些值。 So I tried to write a constructor of Stack which explicitly call the constructor of Node.
因此,我尝试编写一个Stack的构造函数,以显式调用Node的构造函数。 Unfortunately, the compiling error shown above came.
不幸的是,出现了上面显示的编译错误。 I want to know how to call the constructor of a class which is within the other class.
我想知道如何调用另一个类中的一个类的构造函数。
Thank, Kevin Zhou 谢谢,周凯文
This: 这个:
Stack()
{
node.Node();
}
isn't the right way to call a member's constructor. 是调用成员构造函数的正确方法。 Use an initialization list instead:
请使用初始化列表:
Stack(): node()
{
}
It should be noted that explicitly calling the default constructor is not necessary. 应该注意的是,没有必要显式调用默认构造函数。 Which means you only really need to use the initialization list if you're calling a custom constructor.
这意味着,如果要调用自定义构造函数,则只需要真正使用初始化列表即可。
If you don't do anything, the default constructor for the node
member will be called. 如果您不执行任何操作,则将调用该
node
成员的默认构造函数。
If you want to explicitly construct a member, you do it in the constructors initializer list : 如果要显式构造成员,请在构造函数的初始值设定项列表中进行操作 :
Stack()
: node()
{}
Since the default constructor will be called anyway, explicitly constructing members is only useful if you need to pass arguments to the constructors (ie call their non-default constructor). 由于将始终调用默认构造函数,因此仅当您需要将参数传递给构造函数(即,调用其非默认构造函数)时,显式构造成员才有用。
Simply change the definition of the class Stack the following way, because your definition of the class is wrong 只需按照以下方式更改类Stack的定义,因为您对类的定义是错误的
template<class Datatype>
class Stack
{
public:
Stack() : head( nullptr )
{
}
int push(Datatype &);
Datatype pop();
Datatype* peek();
private:
Node<Datatype> *head;
};
Take into account that stack node should have only one reference to the next node. 考虑到堆栈节点应该只对下一个节点有一个引用。 So the class Node should have the following data members
因此,类Node应该具有以下数据成员
class Node
{
//...
private:
Node* next;
Datatype data;
};
Data member Node *prev should be excluded. 数据成员Node * prev应该被排除。
ALso it would be much better if the class Node would be internal class of the class STack. 同样,如果Node类是STack类的内部类,那就更好了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.