简体   繁体   English

无法让构造函数运行

[英]Can't get constructor to run

I'm trying to create a doubly linked list where each list has a first node, last node, and num_elements. 我正在尝试创建一个双向链接列表,其中每个列表都有第一个节点,最后一个节点和num_elements。 However, for some reason, when I try to test the code in a UseList.cpp file, I can't get the num_elements to set to zero as default. 但是,由于某些原因,当我尝试测试UseList.cpp文件中的代码时,无法将num_elements设置为默认值零。

Let me show you what I mean: 让我告诉你我的意思:

In List.h: 在List.h中:

template <class L>
class List
{
   private:
        Node<L> *first;
        Node<L> *last;
        int num_elements;
   public:
        // constructors and destructors
        List();
    [...]
}

[...]

template <class L>
List<L>::List() {
    first = NULL;
    last = NULL;
   num_elements = 0;
}

[...]

This is the show method lower down in list.h: 这是list.h下方的show方法:

template <class L>
// Print out the data in each node separated by a space.
void List<L>::show() {
    cout << num_elements << endl;
    Node<L> *current_node = first;
    while (current_node != NULL) {
       cout << current_node->data << " ";
       current_node = current_node->next;
    }
    cout << endl;
}

Note that there is a cout statement there to print the num_elements. 请注意,这里有一个cout语句来打印num_elements。

This is the relevant part of UseList.cpp: 这是UseList.cpp的相关部分:

int main (int argc, char *argv[]) {
    cout << "-----------------------------------------" << endl;
    cout << "----------------LIST ONE-----------------" << endl;
    cout << "-----------------------------------------" << endl;

    List<int> *list1;
    srand(time(NULL));

    list1->show();
[...]

When show is called, it prints out "1" and gives me a segmentation fault. 调用show时,它打印出“ 1”并给我一个分段错误。 Why is num_elements defaulting to "1" instead of "0"? 为什么num_elements默认为“ 1”而不是“ 0”?

When I do a cout in List<L>::List() { , nothing is printed... (this implies that the constructor never runs?) 当我在List<L>::List() {执行cout时,什么都不会打印出来……(这意味着构造函数永远不会运行?)

Thanks for the help! 谢谢您的帮助!

您正在声明一个指向 List<int>指针 ,而不是将其初始化为任何内容。

You have created a pointer to a List<int> object, but no object. 您已经创建了一个指向List<int>对象的指针,但是没有一个对象。 So, currently, your program will segmentation fault because the pointer is "dangling". 因此,当前,由于指针“悬空”,您的程序将分段错误。 When you try to dereference it with -> , you are accessing memory that isn't yours, and it fails. 当您尝试使用->取消引用时,您正在访问的内存不是您的,它会失败。 To fix this, simply allocate a new List object: 要解决此问题,只需分配一个新的List对象:

List<int> *list1 = new List<int>();

Don't forget to free it later: 不要忘记稍后释放它:

delete list1;

Your other option is to just not use dynamic memory. 您的另一选择是不使用动态内存。 You shouldn't use it if you don't have to. 如果不需要,则不应使用它。

List<int> list1;

list1.show()
List<int> *list1;

Declares list1 to be a pointer. 声明list1为指针。

List<int> *list1 = new List<int>();

Would actually create an instance of List 实际上会创建List的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM