[英]ERROR: SList.cpp:(.text+0x69): undefined reference to `SLNode::SLNode()' while trying to INSERT node to linked list head C++
I am trying to insert a node to the head of a linked list I have created but keep getting an error while trying to compile. 我正在尝试将节点插入到已创建的链表的开头,但是在尝试编译时始终会出现错误。 The error says that I have an undefined reference to the function I am trying to call inside my insertHead function in the SList class. 该错误表明我对要在SList类中的insertHead函数内部调用的函数有未定义的引用。
SList.cpp : 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.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:
/*
* 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.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
,因此我猜测您的compile命令不会编译SLNode.cpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.