对 'Inventory::insertEnd(Node*, int)' 的未定义引用
Undefined reference to 'Inventory::insertEnd(Node*, int)'
我在尝试 build/compile 我的程序时收到“未定义的引用”错误:
obj\Debug\main.o||In function main':|
C:\Users\user1\Desktop\Project5Example\main.cpp|13|undefined reference to
Inventory::insertEnd(Node*, int)'|
...
||error: ld returned 1 exit status|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我对 C++ 很陌生。我究竟做错了什么?我该如何解决?
我觉得与我的头节点有关吗?但无法真正弄清楚它是怎么回事。
错误发生在 main.cpp 行 head = inventory1.insertEnd(head, 8);
这是我的代码:
Inventory.h
#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
Inventory.cpp
#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
应该是 class Inventory
的私有成员。相应地,class Inventory
不应包含具有 return 类型 Node *
的 public 成员函数。所以例如这个成员函数
Node *newNode(int data);
应该删除。反过来这个public成员函数
Node* insertEnd(Node* head, int data);
应该这样声明
void insertEnd( int data );
如果需要(但不是必需),该函数可以调用声明为
的私有静态成员函数
static Node* insertEnd(Node* head, int data);
因为你声明了一个双向单链表,所以将函数 insertEnd
定义为递归函数是没有意义的,因为没有递归。一个新节点 ia 附加到您命名为 trailer
的节点,但最好将其命名为 tail
.
此外,在函数 newNode
和 insertEnd
的定义中,您忘记指定 class 库存的名称,例如
Node * Inventory::newNode(int data)
{
//...
}
Node * Inventory::insertEnd(Node* head, int data)
{
//...
}
这部分在 main
Inventory inventory1;
Node* head = NULL;
head = inventory1.insertEnd(head, 8);
head = inventory1.insertEnd(head, 11);
head = inventory1.insertEnd(head, 20);
没有意义。对象 inventory1
已经包含应该为对象更新的数据成员 head
(和 trailer
)。
class 可以通过下面的演示程序所示的方式定义。
#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
我在尝试 build/compile 我的程序时收到“未定义的引用”错误:
obj\Debug\main.o||In function
main':| C:\Users\user1\Desktop\Project5Example\main.cpp|13|undefined reference to
Inventory::insertEnd(Node*, int)'| ... ||error: ld returned 1 exit status| ||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我对 C++ 很陌生。我究竟做错了什么?我该如何解决? 我觉得与我的头节点有关吗?但无法真正弄清楚它是怎么回事。
错误发生在 main.cpp 行 head = inventory1.insertEnd(head, 8);
这是我的代码:
Inventory.h
#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
Inventory.cpp
#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
应该是 class Inventory
的私有成员。相应地,class Inventory
不应包含具有 return 类型 Node *
的 public 成员函数。所以例如这个成员函数
Node *newNode(int data);
应该删除。反过来这个public成员函数
Node* insertEnd(Node* head, int data);
应该这样声明
void insertEnd( int data );
如果需要(但不是必需),该函数可以调用声明为
的私有静态成员函数static Node* insertEnd(Node* head, int data);
因为你声明了一个双向单链表,所以将函数 insertEnd
定义为递归函数是没有意义的,因为没有递归。一个新节点 ia 附加到您命名为 trailer
的节点,但最好将其命名为 tail
.
此外,在函数 newNode
和 insertEnd
的定义中,您忘记指定 class 库存的名称,例如
Node * Inventory::newNode(int data)
{
//...
}
Node * Inventory::insertEnd(Node* head, int data)
{
//...
}
这部分在 main
Inventory inventory1;
Node* head = NULL;
head = inventory1.insertEnd(head, 8);
head = inventory1.insertEnd(head, 11);
head = inventory1.insertEnd(head, 20);
没有意义。对象 inventory1
已经包含应该为对象更新的数据成员 head
(和 trailer
)。
class 可以通过下面的演示程序所示的方式定义。
#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