Segmentation Fault when trying to push a string to the back of a list.

Posted by user308012 on Stack Overflow See other posts from Stack Overflow or by user308012
Published on 2010-04-02T22:28:24Z Indexed on 2010/04/02 22:33 UTC
Read the original article Hit count: 200

Filed under:

I am trying to write a logger class for my C++ calculator, but I'm experiencing a problem while trying to push a string into a list.

I have tried researching this issue and have found some information on this, but nothing that seems to help with my problem. I am using a rather basic C++ compiler, with little debugging utilities and I've not used C++ in quite some time (even then it was only a small amount).

My code:

#ifndef _LOGGER_H_
#define _LOGGER_H_

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

using std::cout;
using std::cin;
using std::endl;
using std::list;
using std::string;

class Logger
{
private:
 list<string> *mEntries;

public:
 Logger()
 {
  // Initialize the entries list
  mEntries = new list<string>();
 }

 ~Logger()
 {
  // Release the list
  mEntries->clear();
  delete mEntries;
 }

 // Public Methods
 void WriteEntry(string entry)
 {
  // *** BELOW LINE IS MARKED WITH THE ERROR ***
  mEntries->push_back(string(entryData));
 }

 void DisplayEntries()
 {
  cout << endl << "**********************" << endl
            << "*   Logger Entries   *" << endl
      << "**********************" << endl
    << endl;

  for(list<string>::iterator it = mEntries->begin();
   it != mEntries->end(); it++)
  {
   cout << *it << endl;
  }
 }
};

#endif

I am calling the WriteEntry method by simply passing in a string, like so:

mLogger->WriteEntry("Testing");

Any advice on this would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about c++