简体   繁体   English

如何在C ++中的另一个类中调用一个类的构造函数?

[英]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.

相关问题 c ++在同一个类的另一个构造函数中调用构造函数 - c++ call constructor within another constructor of the same class 如何在C ++中的同一类的构造函数中调用重载的构造函数? - How to call an overloaded constructor within a constructor of the same class in c++? 构造函数可以在c ++中调用另一个类的构造函数吗? - Can constructor call another class's constructor in c++? 如何使用构造函数中的参数来调用C ++中另一个类的构造函数? - How to use arguments from constructor to call a constructor of another class in C++? 如何在一个类的构造函数中实例化到另一个类的对象? C ++ - How to instantiate an object to another class in the constructor of a class? C++ 如何在C++中的另一个类中声明一个类的构造函数 - How to declare a constructor of a class inside another class in C++ 错误C2248:受保护的默认构造函数C ++在另一个类所拥有的类中 - Error C2248: Protected Default Constructor C++ within an class owned by another class C++派生类构造函数调用基类构造函数错误 - C++ derived class constructor call base class constructor errors 在C ++中派生类构造函数之后调用基类构造函数 - Call base class constructor after the derived class constructor in C++ c++ 中派生的 class 构造函数中的动态基 class 构造函数调用 - dynamic base class constructor call in derived class constructor in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM