此代码的目标是按价格对购物清单进行排序(然后在价格相同的情况下反向alpha),我甚至无法测试新重写的添加功能。我一直都会遇到这些错误而且不能让我的生活过去:
Undefined symbols for architecture x86_64:
"LinkedList::LinkedList()", referenced from:
_main in main.o
"Node::Node()", referenced from:
LinkedList::add(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, double) in main.o
"Node::~Node()", referenced from:
LinkedList::remove(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, double) in main.o
LinkedList::~LinkedList() in main.o
"Node::~Node()", referenced from:
LinkedList::~LinkedList() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在做了一些研究后,我看到很多人说它与缺少默认构造函数有关,但正如你所看到的,我有我的。至于缺失的符号,我想不出任何已经存在的东西。我仍然是编码的新手(特别是在c ++中),这个截止日期正在我的脖子上呼吸。非常感谢您的帮助!!
// main.cpp
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "LinkedList.hpp"
int main(int argc, const char * argv[]) {
LinkedList list;
list.add("Apple", 1.5);
list.add("Broccoli", 2);
list.add("Yogurt", 4);
list.add("Chocolate", 3.8);
list.add("Paper towels", 2.2);
list.add("Milk", 1.7);
list.add("Ice cream", 4.5);
list.add("Cereal", 4);
std::cout << "Shopping List: \n";
list.displayList();
std::cout << "\n\n";
return 0;
}
和
// LinkedList.hpp
#ifndef LinkedList_hpp
#define LinkedList_hpp
#include <stdio.h>
#include <iostream>
#include <cstdlib>
class Node {
public:
double icost;
std::string iname;
Node *next;
Node();
~Node();
};
class LinkedList : public Node
{
protected:
public:
int count;
Node *head;
Node *tail;
LinkedList();
~LinkedList(); //destructor
void add(std::string name, double cost);
void remove(std::string name, double cost);
void displayList();
};
//------------------------------------------
void LinkedList::add(std::string name, double cost){
Node *nodePtr;
Node *previousNodePtr = nullptr;
bool spotFound;
Node *incomingNode = new Node();
incomingNode->iname = name;
incomingNode->icost = cost;
incomingNode->next = NULL;
if (head == NULL){
head = incomingNode;
tail = incomingNode;
count++;
}
else{
nodePtr = head;
spotFound = false;
while (!spotFound && nodePtr != NULL){
if (nodePtr->iname >= incomingNode->iname){
spotFound = true;
}
else {
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
}
if (nodePtr == head) {
incomingNode->next = head;
head = incomingNode;
count++;
}
else {
previousNodePtr->next = incomingNode;
incomingNode->next = nodePtr;
if (nodePtr == NULL)
tail = incomingNode;
count++;
}
}
}
//------------------------------------------
void LinkedList::remove(std::string name, double cost){
Node *nodePtr, *previousNodePtr = NULL;
if (!head) return;
if (head->icost == cost){
nodePtr = head;
head = head->next;
delete nodePtr;
}
else{
nodePtr = head;
while (nodePtr != NULL && nodePtr->icost != cost){
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr){
previousNodePtr->next = nodePtr->next;
delete nodePtr;
}
}
}
//------------------------------------------
void LinkedList::displayList(){
Node *nodePtr = head;
while (nodePtr){
std::cout << nodePtr->iname << ", " << nodePtr->icost << std::endl;
nodePtr = nodePtr->next;
}
}
//------------------------------------------
LinkedList::~LinkedList(){
Node *nodePtr = head;
while (nodePtr != NULL)
{
Node *garbage = nodePtr;
garbage = nodePtr;
nodePtr = nodePtr->next;
delete garbage;
}
}
//------------------------------------------
#endif /* LinkedList_hpp */