简体   繁体   English

错误:SList.cpp :(。text + 0x69):尝试将节点插入链表头C ++时,未定义对SLNode :: SLNode()的引用

[英]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.

相关问题 使用 main.cpp、node.cpp/.h 和 slist.cpp/.h 构建单链表 (C++) [var SLNode 未命名类型:错误] - Building Singly Linked List (C++), using main.cpp, node.cpp/.h, and slist.cpp/.h [var SLNode does not name a type: error] 尝试使用MinGW编译Assembly + C ++:ccqKAvXJ.o:main.cpp :(。text + 0x18):未定义对“ GetMagicNumber”的引用 - Trying to compile Assembly + C++ with MinGW: ccqKAvXJ.o:main.cpp:(.text+0x18): undefined reference to `GetMagicNumber' 如何在头中的特定节点之后插入链接列表? C ++ - how to insert a linked list after a particular node inside head? c++ 用单链表C++中的头节点交换一个节点 - Swapping one node with the head node in single linked list C++ 当尝试使用C ++编写代码时,我得到一长串错误,例如“/tmp/ccloHU4h.o:dad.cpp:(.text+0x5c):未定义引用`std :: cout&#39;” - When trying to code in C++ I get a long string of errors like “/tmp/ccloHU4h.o:dad.cpp:(.text+0x5c): undefined reference to `std::cout'” 链表头 C++ - Linked List Head C++ c++ 指针操作问题,链表在图的头部插入 - c++ pointer manipulation problem with linked list insert at head for a graph C ++链表不会删除头节点 - C++ linked list does not delete the head node 删除我的链表的头节点? C ++ - Removing the head node of my linked list? C++ 使用双指针头节点初始化链表C ++ - initialize a linked list with a double pointer head node c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM