【问题标题】:Can not output content of the linked list无法输出链表的内容
【发布时间】:2014-05-23 12:36:55
【问题描述】:

我想用文本文件的内容填充linked list

为了实现这一点,我创建了一个结构,该结构将保存文件中一行的数据。

然后我会简单地浏览文件中的行,并在链接的list 中添加填充的结构。

我似乎已经成功地填充了列表,但我无法显示其内容。

这是我目前所拥有的:

Person.h

#ifndef _Person_h_
#define _Person_h_

#include <iostream>
#include <list>
#include <fstream>
#include <string>

class Lista
{
private:
    struct Person
    {
        std::string lastName;
        std::string firstName;
        // other fields ommited for brewity

        Person( const char *lName = "", const char *fName = "" )
        {
            lastName += lName;
            firstName += fName;
        }

        ~Person()
        {
            lastName.clear();
            firstName.clear();
        }
    };

    // my linked list of Persons
    std::list<Person> persons;

public:
    // data comes from a comma delimited file 
    Lista( const char *inputFile, int maxTextLength = 100, char delim = ',' )
    {   
        std::ifstream g;
        g.open(inputFile);

        if( g.is_open() )
        {
            std::string temp( maxTextLength, 0 );

            while( !g.eof() )
            {
                Person p;

                // fill Person structure
                g.getline( &p.lastName[0], maxTextLength, delim );
                g.getline( &p.firstName[0], maxTextLength, delim );

                // add it to the list
                persons.push_back(p);
            }
            g.close();
        }
    }

    // testing function- > it should just display the content of the list
    void print()const
    {
        std::list<Person>::const_iterator it;

        for( it = persons.begin(); it != persons.end(); ++it )
        {
            std::cout << "L: " << it->lastName.c_str() << std::endl
                << "F: " << it->firstName.c_str() << std::endl << std::endl;
        }
    }

    // emty list
    ~Lista(){ persons.clear(); }
};

#endif

我决定在这里停下来测试一下做了什么,所以我在我的 main 中调用了print() 函数:

main.cpp

#include "Person.h"

int main()
{
    Lista p( "test comma separated file.txt", 100, ',' );

    p.print();

    return 0;
}

编译器没有报告错误,但是当我运行我的测试程序时,我得到了这个:

L:
F:

L:
F:

Press any key to continue...

如果我在Lista 构造函数中添加cout,它会正确输出名称。似乎我在构造函数中做错了什么(也许它与临时的Person 变量在堆栈上有关?)但我不知道是什么,因为我没有经验。

你能告诉我我做错了什么吗?

【问题讨论】:

    标签: c++ stl linked-list


    【解决方案1】:

    您需要一个缓冲区来读取。您所做的是创建长度为 1 字节的 lastName 字符串,并最多读取 maxTextLength 字节。

    //create temporary buffers
    char* buffFirstName = new char[maxTextLength];
    char* buffLastName = new char[maxTextLength];
    while( !g.eof() )
    {
        //read into temporary buffers
        g.getline( &buffFirstName[0], maxTextLength, delim );
        g.getline( &buffLastName[0], maxTextLength, delim );
        //create person data
        Person p(buffFirstName, buffLastName);
        // add it to the list
        persons.push_back(p);
    }
    //release the buffers
    delete[] buffFirstName;
    delete[] buffLastName;
    

    【讨论】:

    • 在C++中,你不能写char buffFirstName[maxTextLength];,因为它会报错。尽管如此,我还是使用strings 绕过了它。谢谢你。赞成。
    • @user3261013 哦,对不起我的错误。我认为maxTextLenght 是一个常数或#define。重点是当您将动态分配类用作char* 时,它们会失去所有魔力。我希望你在传递给getline之前已经完成了string.reserve(maxTextLength)
    【解决方案2】:
    Person( const char *lName = "", const char *fName = "" )
        {
            lastName += lName;
            firstName += fName;
        }
    

    不确定,但在 c++ 中,你能用这个作为参数吗? :

    const char *lName = ""
    

    我了解您想要什么(默认值),但它将值设置为“”。 尝试不使用它。

    【讨论】:

    • 结果还是一样。
    • 您可以使用 gdb 之类的错误跟踪器来逐步查看发生了什么。另外,你不应该使用 $this->lastName += lName 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2011-12-26
    • 2016-02-13
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多