【发布时间】:2017-01-06 17:42:57
【问题描述】:
我将首先提出问题,然后包含所有代码。我正在学习数据结构课程,我们提供了一个 make 文件,但我的 C++ 技能充其量只是平庸。我已经翻阅了许多不同的文章,但我仍然不知道如何集成代码以创建新节点......我必须能够创建新节点才能将它们入队、出队和执行其他操作对它们的操作与双向链表有关。
我尝试了许多不同的方法来构建构造函数,但似乎总是存在某种类型的编译器错误。
我不允许更改主文件中的任何代码,但我的所有工作都必须包含在模板化的头文件中。有人可以告诉我如何正确地做到这一点吗?我现在将包含相关代码。
这是我的头文件。我的目标是让 LinkedList 类中的 buildNode() 函数创建一个节点,然后我可以将节点添加到双向链表中。
#include <limits>
#include <string>
#include <cassert>
#include <iostream>
template <typename T>
class Node {
public:
T data;
Node* next;
Node* prev;
Node();
~Node();
void getData() {
}
};
template <typename T>
class Iterator {
private:
public:
Iterator() {
}
int operator*() const {
}
Iterator& operator++() {
}
bool operator==(Iterator const& rhs) {
}
bool operator!=(Iterator const& rhs) {
}
};
template <typename T>
class LinkedList {
private:
Node<T> *head = 0;
Node<T> *tail = 0;
public:
LinkedList() {
}
~LinkedList() {}
Iterator<T> begin() const {
}
Iterator<T> end() const {
}
bool isEmpty() const {
if (this->head == 0) {
std::cout << "Isempty works.";
return true;
}
}
T getFront() const {
}
T getBack() const {
}
void enqueue (T element) {
}
void dequeue() {
}
void pop() {
}
void clear() {
}
bool contains(int element) const {
}
void remove(int element) {
}
void buildNode() { //experimental function.
Node<T> n;
// Node n = new Node();
// Node<T> n;
//Node<T> *n = new Node<T>();
//Node n = new Node<T>;
}
};
我将只包含对这个特定挑战很重要的主文件元素,即
int main()
{
// Get ready.
LinkedList<string&> referenceList;
LinkedList<char const*> valueList;
//ascribe valueList and referenceList to a variable inside the LinkedList class...
unsigned int numOfStrings = 8;
string testStrings[] = {
"alpha"
, "bravo"
, "charlie"
, "charlie"
, "dog"
, "echo"
, "foxtrot"
, "golf"
};
string tempStr;
// Test isEmpty function.
assert(valueList.isEmpty() && referenceList.isEmpty());
referenceList.buildNode(); //can't get this to work. Later the methodology will be transported to enqueue method.
编辑:当我使用
Node<T> * n = new Node<T>;
在 buildNode() 函数中,我得到一个链接器错误说明:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/Boisselle/Library/Caches/CLion2016.2/cmake/generated/LinkedList-75be2cc8/75be2cc8/Debug --target LinkedList -- -j 8
Scanning dependencies of target LinkedList
[ 50%] Building CXX object CMakeFiles/LinkedList.dir/main.cpp.o
[100%] Linking CXX executable LinkedList
Undefined symbols for architecture x86_64:
"Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>::Node()", referenced from:
LinkedList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>::buildNode() in main.cpp.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[3]: *** [LinkedList] Error 1
make[2]: *** [CMakeFiles/LinkedList.dir/all] Error 2
make[1]: *** [CMakeFiles/LinkedList.dir/rule] Error 2
make: *** [LinkedList] Error 2
【问题讨论】: