我正试图将一个节点插入到我创建的链表的头部,但在尝试编译时一直出现错误。该错误表明,我在SList类的insertHead函数中有一个未定义的引用,指向我试图调用的函数。
SList.cpp:
/*
* SList.cpp
*
* written by Carlos D. Escobedo
* created on 26 Oct
*
* References:
*/
#include "SList.h"
SList::SList() {
head = NULL;
size = 0;
}
SList::~SList() {
SList::clear();
}
void SList::insertHead(int value) {
if (head == NULL) {
head = new SLNode();
} else {
}
}
void SList::removeHead() {
if (head != NULL)
head = NULL;
}
void SList::clear() {
head = NULL;
}
unsigned int SList::getSize() const {
return size;
}
string SList::toString() const {
stringstream ss;
/*
if (head == NULL) {
return "";
} else {
for (int i = 0; i < (size-1); i++) {
ss << head[i] << ", ";
}
ss << head[size-1];
}
*/
return "hello";
}
SList.h:
/*
* SList.cpp
*
* written by Carlos D. Escobedo
* created on 26 Oct
*
* References:
*/
#ifndef SLIST_H
#define SLIST_H
#include "SLNode.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class SLNode;
class SList {
public:
SList();
~SList();
void insertHead(int value);
void removeHead();
void clear();
unsigned int getSize() const;
string toString() const;
private:
SLNode* head;
unsigned int size;
};
#endif
SLNode.cpp:
/*
* SLNode.cpp
*
* written by Carlos D. Escobedo
* created on 20 oct
*
* References:
*/
#include "SLNode.h"
SLNode::SLNode() {
nextNode = NULL;
contents = 0;
}
SLNode::SLNode(int value) {
nextNode = NULL;
contents = value;
}
SLNode::~SLNode() {
nextNode = NULL;
}
void SLNode::setContents(int newContent) {
contents = newContent;
}
int SLNode::getContents() const {
return contents;
}
void SLNode::setNextNode(SLNode* newNode) {
nextNode = newNode;
}
SLNode* SLNode::getNextNode() const {
return nextNode;
}
SLNode.h:
/*
* SLNode.cpp
*
* written by Carlos D. Escobedo
* created on 20 oct
*
* References:
*/
#ifndef SLNODE_H
#define SLNODE_H
class SList;
class SLNode {
public:
SLNode();
SLNode(int contents);
~SLNode();
void setContents(int newContent);
int getContents() const;
void setNextNode(SLNode* newNode);
SLNode* getNextNode() const;
private:
SLNode* nextNode;
int contents;
};
#endif
Makefile:
# Target for programming challenge-18
# Date completed: 10-26-2015
pc18: pc18.cpp SList.cpp SList.h SLNode.cpp SLNode.h
g++ -o challenge-18 pc18.cpp SList.cpp SLNode.h
由于SLNode
仅在SList.cpp
中使用,我猜您的编译命令不会编译SLNode.cpp