【问题标题】:Allocation of data through a list of pointers in C++通过 C++ 中的指针列表分配数据
【发布时间】:2016-05-24 14:48:34
【问题描述】:

我正在尝试从一个文件中读取多个对象的数据。读取本身工作正常,但我无法将数据写入std::list<MyObject*>。我尝试了几件事,但它总是以打印一些东西后应用程序崩溃而告终,我认为这是随机内存内容。您可以在下面找到当前版本。我也尝试过迭代器,但也没有用。 以下是调用方法中发生的情况:

    PAProject proj2 = PAProject();
    proj2.projectName = "myfirstOne";

    PaFigureLoader::loadFigures(&proj2);

    std::list<PAFigure*>::iterator figIterator;
    for(figIterator = proj2.figures.begin();
            figIterator != (proj2.figures.end());
            figIterator++) {
        PAFigure* fig = *figIterator;
        if(fig->firstname.empty()) {
            std::cout << "Nothing to see here\n";
            break;
        } else {
            std::cout << fig->firstname << "\n";
        }
    }

名单是std::list&lt;PAFigure*&gt; figures

造成所有麻烦的方法:

static void loadFigures(PAProject *project)
{
        //open input stream
    fs::ifstream ifstream(filename);
    std::string input;
    ifstream >> input;
    PAFigure * fig;

    //keep a string for all the splitting:
    std::string result;

    //define regexs
    std::regex reg1 ("(<firstname>)([a-zA-Z0-9]*)(</firstname>)");
    std::regex reg2 ("(<lastname>)([a-zA-Z0-9]*)(</lastname>)");

    //iterate through file to find all figures
    while(input.compare("</allfigures>") != 0) {

        //do the figure-loading stuff
        if(input.compare("<figure>")==0) {
            PAFigure newFigure = PAFigure();
            project->figures.push_front(&newFigure);
            fig = &newFigure;
        }

        //find contents
        if(input.find("<firstname>")!= std::string::npos) {
            std::regex_replace (std::back_inserter(result), input.begin(), input.end(), reg1, "$2");
            fig->firstname = result;
            result.erase();
            std::cout << fig->firstname << " is firstname\n";
        }
        if(input.find("<lastname>")!= std::string::npos) {
            std::regex_replace (std::back_inserter(result), input.begin(), input.end(), reg2, "$2");
            fig->lastname = result;
            result.erase();
        }

        //read next line
        ifstream >> input;
    }
    PAFigure * figtest = project->figures.back();
    std::cout << figtest->firstname << " last element\n";

    figtest = project->figures.front();
    std::cout << figtest->firstname << " first element\n";
}

这是输出:

 died
Anna is firstname
 died
Dorian is firstname
Dorian last element
Dorian first element
Dorian died
Anna died

我加了

PAFigure::~PAFigure()
{
   std::cout << this->firstname << " died\n";

} 

因为我有一种奇怪的感觉,我的元素就这样消失了,而且显然PAFigure newFigure = PAFigure() 确实没有名字。

我承认我的 C++ 编码经验,尤其是指针/引用方面的经验是非常...基本的。我不知道如何解决这个问题,甚至没有谈论以优雅的方式解决它。

【问题讨论】:

  • 投资学习断点/单步调试...你会发现到底发生了什么。
  • @krOoze 我对断点和调试很熟悉,但是发布起来不太方便,而且我知道出了什么问题,只是无法解决。

标签: c++ list pointers memory reference


【解决方案1】:

使用指针而不是引用。

这里有什么问题:

PAFigure newFigure = PAFigure();

你应该使用指针来推送,而不是引用。

PAFigure *newFigure = new PAFigure();
project->figures.push_front(newFigure);
// and create another object too.
PAFigure *fig = new PAFigure();

【讨论】:

  • 谢谢,完成了这项工作。
猜你喜欢
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 2015-03-20
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多