尝试构建/编译程序时出现“ undefined reference ”错误:



我刚接触c++。我究竟做错了什么?以及如何解决?
我觉得这与我的头节点有关吗?但无法真正弄清楚它是什么。

错误发生在main.cpp行head = inventory1.insertEnd(head, 8);

这是我的代码:

库存

#ifndef INVENTORY_H
#define INVENTORY_H

#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>


using namespace std;

        struct Node
        {
            int data;
            Node* next;
        };

class Inventory
{
    public:
        // Default Constructor
        Inventory();

        // MODIFICATION MEMBER FUNCTIONS
        Node *newNode(int data);
        Node* insertEnd(Node* head, int data);

    private:
        // Data members
        Node *head;
        Node *trailer;
};

#endif // INVENTORY_H


库存文件

#include "Inventory.h"
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <bits/stdc++.h>

using namespace std;

Inventory::Inventory()
{
    // Set the header and trailer to NULL
    head = NULL;
    trailer = NULL;
}

// Allocates a new node with given data
Node *newNode(int data)
{
    Node *new_node = new Node;
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

// Function to insert a new node at the
// end of linked list using recursion.
Node* insertEnd(Node* head, int data)
{
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == NULL)
        return newNode(data);

    // If we have not reached end, keep traversing
    // recursively.
    else
        head->next = insertEnd(head->next, data);
    return head;
}

main.cpp

#include <iostream>
#include "Inventory.h"

using namespace std;

int main()
{
    // Create an inventory list
    Inventory inventory1;

    Node* head = NULL;

    head = inventory1.insertEnd(head, 8);
    head = inventory1.insertEnd(head, 11);
    head = inventory1.insertEnd(head, 20);

    return 0;
}

最佳答案

对于初学者,结构Node应该是Inventory类的私有(private)成员。相应地,类Inventory不应包含具有返回类型Node *的公共(public)成员函数。例如这个成员函数

Node *newNode(int data);

应该删除。反过来,此公共(public)成员函数
Node* insertEnd(Node* head, int data);

应该像
void insertEnd( int data );

如果需要(但不是必需的),该函数可以调用一个私有(private)的静态成员函数,声明为
static Node* insertEnd(Node* head, int data);

声明了双向单链列表时,将insertEnd函数定义为递归函数是没有意义的,因为没有递归。尽管可以将其命名为trailer,但会在您命名的节点上附加一个新节点,例如tail

此外,在函数newNodeinsertEnd的定义中,您忘记了指定 list 类的名称,例如
Node * Inventory::newNode(int data)
{
    //...
}

Node * Inventory::insertEnd(Node* head, int data)
{
    //...
}

而这部分主要
Inventory inventory1;

Node* head = NULL;

head = inventory1.insertEnd(head, 8);
head = inventory1.insertEnd(head, 11);
head = inventory1.insertEnd(head, 20);

没有道理。对象inventory1已经包含应为该对象更新的数据成员head(和trailer)。

例如,可以按照以下演示程序中所示的以下方式定义类。
#include <iostream>

class Inventory
{
public:
    Inventory() = default;

    Inventory( const Inventory & ) = delete;
    Inventory & operator =( const Inventory & ) = delete;

    ~Inventory();

    void insertEnd( int data );

    void clear();

    friend std::ostream & operator <<( std::ostream &, const Inventory & );

private:
    struct Node
    {
        int data;
        Node *next;
    } *head = nullptr, *tail = nullptr;
};

Inventory::~Inventory()
{
    clear();
}

void Inventory::insertEnd( int data )
{
    Node *node = new Node { data, nullptr };

    if ( tail == nullptr )
    {
        head = tail = node;
    }
    else
    {
        tail = tail->next = node;
    }
}

void Inventory::clear()
{
    while ( head != nullptr )
    {
        Node *node = head;
        head = head->next;
        delete node;
    }

    tail = head;
}

std::ostream & operator <<( std::ostream &os, const Inventory &inventory )
{
    for ( Inventory::Node *node = inventory.head; node != nullptr; node = node->next )
    {
        os << node->data << " -> ";
    }

    return os << "null";
}

int main()
{
    Inventory inventory;

    for ( const auto &data : { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } )
    {
        inventory.insertEnd( data );
    }

    std::cout << inventory << '\n';

    return 0;
}

程序输出为
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null

10-08 18:53