- 终端
g++ -std=c++11 -g -Wall -c main.cpp
g++ -std=c++11 -g -Wall -c LinkedListTester.cpp
g++ -std=c++11 -g -Wall -c Executive.cpp
g++ -std=c++11 -g -Wall -c Node.cpp
g++ -std=c++11 -g -Wall -c LinkedList.cpp
g++ -std=c++11 -g -Wall main.o LinkedListTester.o Executive.o -o HelloWorld
Undefined symbols for architecture x86_64:
"LinkedList::insert(int, int)", referenced from:
LinkedListTester::test02() in LinkedListTester.o
LinkedListTester::test03() in LinkedListTester.o
LinkedListTester::test04() in LinkedListTester.o
LinkedListTester::test05() in LinkedListTester.o
LinkedListTester::test06() in LinkedListTester.o
LinkedListTester::test07() in LinkedListTester.o
LinkedListTester::test08() in LinkedListTester.o
...
"LinkedList::remove(int)", referenced from:
LinkedListTester::test05() in LinkedListTester.o
LinkedListTester::test06() in LinkedListTester.o
LinkedListTester::test07() in LinkedListTester.o
Executive::interactive_mode() in Executive.o
"LinkedList::replace(int, int)", referenced from:
Executive::interactive_mode() in Executive.o
"LinkedList::LinkedList()", referenced from:
LinkedListTester::test01() in LinkedListTester.o
LinkedListTester::test02() in LinkedListTester.o
LinkedListTester::test03() in LinkedListTester.o
LinkedListTester::test04() in LinkedListTester.o
LinkedListTester::test05() in LinkedListTester.o
LinkedListTester::test06() in LinkedListTester.o
LinkedListTester::test07() in LinkedListTester.o
...
"LinkedList::~LinkedList()", referenced from:
Executive::~Executive() in main.o
LinkedListTester::test01() in LinkedListTester.o
LinkedListTester::test02() in LinkedListTester.o
LinkedListTester::test03() in LinkedListTester.o
LinkedListTester::test04() in LinkedListTester.o
LinkedListTester::test05() in LinkedListTester.o
LinkedListTester::test06() in LinkedListTester.o
...
"LinkedList::isEmpty() const", referenced from:
LinkedListTester::test01() in LinkedListTester.o
"LinkedList::getEntry(int) const", referenced from:
Executive::interactive_mode() in Executive.o
"LinkedList::getLength() const", referenced from:
LinkedListTester::test02() in LinkedListTester.o
LinkedListTester::test03() in LinkedListTester.o
LinkedListTester::test04() in LinkedListTester.o
LinkedListTester::test05() in LinkedListTester.o
LinkedListTester::test06() in LinkedListTester.o
LinkedListTester::test07() in LinkedListTester.o
LinkedListTester::test08() in LinkedListTester.o
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make: *** [HelloWorld] Error 1
我输入了make进行编译,但出现上述错误 下面是我的LinkedList.cpp和LinkedList.h代码。另外,还提供了makefile以防万一。
- LinkedList.cpp
#include "LinkedList.h"
LinkedList::LinkedList() {
m_front = nullptr;
m_length = 0;
}
LinkedList::LinkedList(const LinkedList &original) {
m_length = original.getLength();
if(m_length == 0) {
m_front = nullptr;
}
Node* temp = m_front;
Node* temp1 = m_front;
for (int i = 2; i <= m_length; i++) {
temp -> setEntry(original.getEntry(i));
temp1 -> setNext(temp);
temp1 = temp;
}
}
LinkedList::~LinkedList() {
clear();
}
LinkedList& LinkedList::operator=(const LinkedList &original) {
this -> clear();
m_length = original.getLength();
m_front -> setEntry(original.getEntry(1));
for (int i = 2; i <= m_length; i++) {
this -> insert(i, original.getEntry(i));
}
return (*this);
}
bool LinkedList::isEmpty() const {
return m_front == nullptr;
}
int LinkedList::getLength() const {
return m_length;
}
void LinkedList::insert(int position, int entry) {
if (position < 1 || position > getLength() + 1)
throw runtime_error("Position out of range!");
Node* current = m_front;
Node* previous = m_front;
for (int i = 1; i < position; i++) {
previous = current;
current = current -> getNext();
}
Node* n = new Node(entry);
n -> setNext(current);
if (position == 1) m_front = n;
else previous -> setNext(n);
m_length++;
}
void LinkedList::remove(int position) {
if (position < 1 || position > getLength())
throw runtime_error("Position out of range!");
Node* current = m_front;
Node* previous = m_front;
for (int i = 1; i < position; i++) {
previous = current;
current = current -> getNext();
}
if (position == 1) {
m_front = current -> getNext();
} else previous -> setNext(current -> getNext());
delete current;
m_length--;
}
void LinkedList::clear() {
if (!isEmpty()) {
while(m_front != nullptr) {
Node* temp = m_front;
m_front = m_front -> getNext();
delete(temp);
}
m_length = 0;
}
}
int LinkedList::getEntry(int position) const {
if (position < 1 || position > getLength())
throw runtime_error("Position out of range!");
Node* temp = m_front;
for (int i = 1; i < position; i++) {
temp = temp -> getNext();
}
return temp -> getEntry();
}
void LinkedList::replace(int position, int newEntry) {
if(position < 1 || position > getLength())
throw runtime_error("Position out of range!");
Node* temp = m_front;
for (int i = 1; i < position; i++) {
temp = temp -> getNext();
}
temp -> setEntry(newEntry);
}
//LinkedList.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include "Node.h"
#include <iostream>
#include <stdexcept> //For runtime_error
using namespace std;
class LinkedList {
private:
Node *m_front;
int m_length;
public:
/**
* @pre True
* @post set value of m_front to nullptr and m_length to 0
**/
LinkedList();
/**
* @pre A list
* @post creates a list
* @param original: pass in the desired list
**/
LinkedList(const LinkedList &original);
/** Destructor
* @pre True
* @post deletes all nodes in the list
**/
~LinkedList();
/**
* @pre A list
* @post copies the list into another empty list
* @param original: pass in the desired list to be copied by =
**/
LinkedList &operator=(const LinkedList &original);
/**
* @pre True
* @post returns true if the list is empty otherwise false
**/
bool isEmpty() const;
/**
* @pre True
* @post Returns the value of the list's length
**/
int getLength() const;
/**
* @pre The position is between 1 and the list's length + 1
* @post Inserts a node in a desired position
* @param position: 1 <= position <= length + 1
* @param entry: An entry to insert in the list
* @throw std::runtime_error if the position is invalid
**/
void insert(int position, int entry);
/**
* @pre The position is between 1 and the list's length
* @post Removes a node in the position
* @param position: 1 <= position <= length
* @throw std::runtime_error if the position is invalid
**/
void remove(int position);
/**
* @pre True
* @post delete all nodes in the list
**/
void clear();
/**
* @pre The position is between 1 and the list's length
* @post The entry at the position is returned
* @param position: 1 <= position <= length
* @throw std::runtime_error if the position is invalid
**/
int getEntry(int position) const;
/**
* @pre The position is between 1 and the list's length
* @post The entry at the given position is replaced with the new entry
* @param position: 1 <= position <= length
* @param newEntry: A new entry to put in the list
* @throw std::runtime_error if the position is invalid.
**/
void replace(int position, int newEntry);
};
#endif
// makefile
HelloWorld: main.o LinkedListTester.o Executive.o Node.o LinkedList.o
g++ -std=c++11 -g -Wall main.o LinkedListTester.o Executive.o -o
HelloWorld
main.o: main.cpp LinkedListTester.h Executive.h
g++ -std=c++11 -g -Wall -c main.cpp
LinkedListTester.o: LinkedListTester.h LinkedListTester.cpp
g++ -std=c++11 -g -Wall -c LinkedListTester.cpp
Executive.o: Executive.h Executive.cpp
g++ -std=c++11 -g -Wall -c Executive.cpp
LinkedList.o: LinkedList.h LinkedList.cpp
g++ -std=c++11 -g -Wall -c LinkedList.cpp
Node.o: Node.h Node.cpp
g++ -std=c++11 -g -Wall -c Node.cpp
clean:
rm *.o HelloWorld
我试图通过检查更新和所有内容来找出问题所在,但我无法找到关于架构x86_64错误的未定义符号。