简体   繁体   中英

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.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 ,因此我猜测您的compile命令不会编译SLNode.cpp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM