C++ 将文件读入链表C++;具有类中的节点结构

C++ 将文件读入链表C++;具有类中的节点结构,c++,pointers,object,linked-list,member-function-pointers,C++,Pointers,Object,Linked List,Member Function Pointers,我试图将包含字母“farming”的文本文件读入节点链接列表。我创建了一个名为NumberList的类,它具有节点的结构。这是标题 #ifndef NUMBERLIST #define NUMBERLIST #include <iostream> using namespace std; class NumberList { protected: //declare a class for the list node //constructor to initialize nod

我试图将包含字母“farming”的文本文件读入节点链接列表。我创建了一个名为NumberList的类,它具有节点的结构。这是标题

#ifndef NUMBERLIST
#define NUMBERLIST
#include <iostream>

using namespace std;

class NumberList
{
protected:
//declare a class for the list node
//constructor to initialize nodes of list
struct ListNode
{
    char value;
    ListNode *next;

    // Constructor 
    ListNode(char value1, ListNode *next1 = NULL)
    {
        value = value1;
        next = next1;
    }
};

ListNode *head;  //pointer to head of the list

public:
NumberList() { head = NULL; }  //constructor 
~NumberList();      //destructor
void displayList() const;  //print out list
void reverse();

};
#endif
\ifndef NUMBERLIST
#定义数字列表
#包括
使用名称空间std;
类数学家
{
受保护的:
//为列表节点声明一个类
//构造函数初始化列表的节点
结构列表节点
{
字符值;
ListNode*下一步;
//建造师
ListNode(字符值1,ListNode*next1=NULL)
{
值=值1;
next=next1;
}
};
ListNode*head;//指向列表头的指针
公众:
NumberList(){head=NULL;}//构造函数
~NumberList();//析构函数
void displayList()const;//打印输出列表
无效反向();
};
#恩迪夫
我遇到的问题是试图将文本文件读入main()中的链接列表

以下是我的主要观点:

#include "Numberlist.h"
#include "ReliableNumberList.h"
#include <iostream>
#include <fstream>


using namespace std;

int main()
{

ListNode *letterList = nullptr;  //create a linked list
char letter;
                    //This is where I read the file into the list
//open the file
ifstream letterFile("linkedText.txt");
if (!letterFile)
{
    cout << "Error in opening the file of letters.";
    exit(1);
}
//read the file into a linked list
while (letterFile >> letter)
{
    //create a node to hold this letter
    letterList = new ListNode(letter, letterList);
    //missing a move to the next node?
}
return 0;
}
#包括“Numberlist.h”
#包括“ReliableNumberList.h”
#包括
#包括
使用名称空间std;
int main()
{
ListNode*letterList=nullptr;//创建链接列表
字符字母;
//这是我将文件读入列表的地方
//打开文件
ifstream-lettfile(“linkedText.txt”);
如果(!letterFile)
{
cout>字母)
{
//创建一个节点来保存此字母
letterList=新列表节点(字母,字母列表);
//缺少到下一个节点的移动?
}
返回0;
}
此读取文件示例来自我的课本,但其读取到的结构不在单独的类中。就我而言,我无法确定如何在NumberList类中引用ListNode结构。Visual Studio声明ListNode和letterList未定义。我知道这一点,因为我没有从Nu中正确引用它们姆贝利斯特类


非常感谢您的帮助。

您的问题的快速解决方案如下:

//------------------------------NumberList.hpp-----------------------------

#ifndef NUMBERLIST_HPP
#define NUMBERLIST_HPP
#include <iostream>

class NumberList{
protected:
    //Protected Members can't be used outside the class
    struct ListNode{
        char value;
        ListNode *next;
        ListNode(char value1, ListNode *next1 = NULL){
            value = value1;
            next = next1;
        }
    };
    ListNode *head, *tail;  //class members
    //head always points at the 1st letter, tail is used for quick adding at the end
public:
    NumberList() { head = NULL; tail = NULL; }
    ~NumberList(); //don't forget to deallocate space properly at the end
    void displayList() const;  //print out list
    void reverse();
    void add(char newchar) {
        //allocate a new node using the ListNode constructor, by default, next1 will be null
        ListNode *newNode = new ListNode(newchar); //equvalent to ListNode(newchar, NULL);
        if (tail == NULL) { //if no elements in the list, both show to newNode
            tail = newNode;
            head = newNode;
        }else{
            tail->next = newNode; //make last node -> next pointer, point to newNode (new last node)
            tail = tail->next; //make current last node be the actual last node
        }
    }
};
#endif

//------------------------------Main.cpp-----------------------------
#include "Numberlist.hpp"
#include <iostream>
#include <fstream>

using namespace std;

int main(){
    ifstream letterFile("linkedText.txt");
    if (!letterFile){
        cout << "Error in opening the file of letters.";
        exit(-1);
    }

    NumberList numberList;

    char letter;
    while (letterFile >> letter) numberList.add(letter);
}
/------------------------------------NumberList.hpp-----------------------------
#ifndef数字列表水电站
#定义NUMBERLIST_水电站
#包括
类数学家{
受保护的:
//受保护的成员不能在类外使用
结构列表节点{
字符值;
ListNode*下一步;
ListNode(字符值1,ListNode*next1=NULL){
值=值1;
next=next1;
}
};
ListNode*head,*tail;//类成员
//头始终指向第一个字母,尾用于在末尾快速添加
公众:
NumberList(){head=NULL;tail=NULL;}
~NumberList();//别忘了在末尾正确释放空间
void displayList()const;//打印输出列表
无效反向();
void add(char newchar){
//使用ListNode构造函数分配一个新节点,默认情况下,next1为空
ListNode*newNode=newlistnode(newchar);//与ListNode相等(newchar,NULL);
如果(tail==NULL){//如果列表中没有元素,则两者都显示给newNode
tail=newNode;
头=新节点;
}否则{
tail->next=newNode;//生成最后一个节点->下一个指针,指向newNode(新的最后一个节点)
tail=tail->next;//使当前最后一个节点成为实际的最后一个节点
}
}
};
#恩迪夫
//------------------------------Main.cpp-----------------------------
#包括“Numberlist.hpp”
#包括
#包括
使用名称空间std;
int main(){
ifstream-lettfile(“linkedText.txt”);
如果(!letterFile){
cout>字母)数字列表。添加(字母);
}
它会稍微改变您的逻辑,因为您不再向列表中添加列表节点, 但我怀疑你无论如何都不想。相反,最好添加字符 直接连接到列表,并让列表处理其节点(这是有意义的,因为节点结构是受保护的)


当然,该类需要进一步细化,但这应该可以解决您最初的问题。

欢迎使用Stack Overflow。请花点时间阅读并参考您可以在此处询问的内容和方式。
ListNode
struct似乎受到外部世界的
保护。其想法可能是
NumberList
将有一种方法(我们称之为
push_back(…)
)将
char
s添加到内部(受保护/私有)列表中。这样
NumberList
对象将管理节点的创建和销毁,在
main()
中,您只需要编写漂亮的
列表。push_back(字母)
,而不是每次都直接创建
ListNode
。此外
ListNode
实际上是
NumberList::ListNode
ListNode
NumberList
中,不必完全限定,因为它是
NumberList
。非常感谢!这真是一点我告诉了我正确的方向。我为我的类创建了一个add方法,正如你在上面概述的那样。通过一点工作,我就能够让作业的所有内容都正常工作。