Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用main.cpp、node.cpp/.h和slist.cpp/.h[var-SLNode未命名类型:错误]构建单链接列表(C+;+;)_C++_Class_Linked List_Nodes - Fatal编程技术网

C++ 使用main.cpp、node.cpp/.h和slist.cpp/.h[var-SLNode未命名类型:错误]构建单链接列表(C+;+;)

C++ 使用main.cpp、node.cpp/.h和slist.cpp/.h[var-SLNode未命名类型:错误]构建单链接列表(C+;+;),c++,class,linked-list,nodes,C++,Class,Linked List,Nodes,我正在构建一个单链接列表,它使用main.cpp并具有两个其他文件作为依赖项。一个是具有节点类的SLNode.cpp/.h文件,另一个是具有单链接列表类的SList.cpp/.h文件。我遇到的问题是,当我尝试编译时,终端说:“在SLNode.h:13:0:SList.h:31:5:error中包含的文件中:'SLNode'没有命名类型SLNode*head;” 多亏了我在评论部分收到的反馈,这个问题已经解决了。现在的新问题是,当我尝试编译时,终端会给我以下错误: SList.cpp:(.text

我正在构建一个单链接列表,它使用main.cpp并具有两个其他文件作为依赖项。一个是具有节点类的SLNode.cpp/.h文件,另一个是具有单链接列表类的SList.cpp/.h文件。我遇到的问题是,当我尝试编译时,终端说:“在SLNode.h:13:0:SList.h:31:5:error中包含的文件中:'SLNode'没有命名类型SLNode*head;”

多亏了我在评论部分收到的反馈,这个问题已经解决了。现在的新问题是,当我尝试编译时,终端会给我以下错误:

SList.cpp:(.text+0x49):对'SLNode::~SLNode()'的未定义引用

但我认为这只是我糟糕的编程和我没有正确编写代码的事实

main.cpp(称为pc18.cpp):

SLNode.cpp:

/*
 * SLNode.cpp
 * 
 * written by Carlos D. Escobedo
 * created on 20 oct
 * 
 * References: 
 */
#include "SLNode.h"
#include <iostream>

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文件中删除#include“SList.h”语句。它是不需要的,我认为这就是导致错误的原因。

您有循环包含。slnode.h包括slist.h。slist.h包括slnode.h,由于包含防护,因此跳过了slnode.h。因此,当稍后在slist.h中遇到SNode名称时,该类还没有被声明,您将得到错误


因为slnode.h头不使用slist.h中的任何内容,所以不要包含它。另一种方法是使用
类SList
向前声明类。

您的结构相互引用,您输入的条件编译意味着SList或SLNode将在另一个之前定义

更改include顺序或更改条件编译并不能解决这个问题——只需解决这个问题

相反,尝试像这样对结构/类进行正向声明

class SLNode;
class SList {
    ....

同样,在定义
SLNode
之前,执行
SList
的前向声明

谢谢你的快速回复。我从SLNode.h文件中删除了#include“SList.h”,但这只是生成了我以前遇到的错误,即:g++-o challenge-18 pc18.cpp SList.cpp SLNode.h/tmp/ccudags.o:In function
SList::~SList():SList.cpp:(.text+0x49):未定义对
SLNode:~SLNode()的引用,我为格式道歉,我还不知道如何格式化评论。谢谢你的回复!当我从SLNode.h文件中获取#include“SList.h”并按照您告诉我的那样声明forward时,我仍然收到一个错误,即SList.cpp:(.text+0x49):对`SLNode::~SLNode()'的未定义引用?你认为这更像是一个makefile问题吗?如果不知道你的makefile和你修改过的代码以及包含的内容,很难说——如果你有新问题,你应该问一个新问题
/*
 * SLNode.cpp
 * 
 * written by Carlos D. Escobedo
 * created on 20 oct
 * 
 * References: 
 */
#include "SLNode.h"
#include <iostream>

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;
}
//SList.h

#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
/*
 * 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();
    delete head;
}

void SList::insertHead(int value) {
    head = new SLNode(value);
}

void SList::removeHead() {
    if (head != NULL) {
        delete head;            
    }
}

void SList::clear() {
    delete head;
}

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";
}
# 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
class SLNode;
class SList {
    ....