Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation Fault when trying to push a string to the back of a list
    text
    copied!<p>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.</p> <p>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).</p> <p>My code:</p> <pre><code>#ifndef _LOGGER_H_ #define _LOGGER_H_ #include &lt;iostream&gt; #include &lt;list&gt; #include &lt;string&gt; using std::cout; using std::cin; using std::endl; using std::list; using std::string; class Logger { private: list&lt;string&gt; mEntries; public: Logger() {} ~Logger() {} // Public Methods void WriteEntry(const string&amp; entry) { mEntries.push_back(entry); } void DisplayEntries() { cout &lt;&lt; endl &lt;&lt; "**********************" &lt;&lt; endl &lt;&lt; "* Logger Entries *" &lt;&lt; endl &lt;&lt; "**********************" &lt;&lt; endl &lt;&lt; endl; for(list&lt;string&gt;::iterator it = mEntries.begin(); it != mEntries.end(); it++) { // *** BELOW LINE IS MARKED WITH THE ERROR *** cout &lt;&lt; *it &lt;&lt; endl; } } }; #endif </code></pre> <p>I am calling the WriteEntry method by simply passing in a string, like so:</p> <pre><code>mLogger-&gt;WriteEntry("Testing"); </code></pre> <p>Any advice on this would be greatly appreciated.</p> <p><strong>* CODE ABOVE HAS BEEN ALTERED TO HOW IT IS NOW *</strong></p> <p>Now, the line:</p> <pre><code>cout &lt;&lt; *it &lt;&lt; endl; </code></pre> <p>causes the same error. I'm assuming this has something to do with how I am trying to get the string value from the iterator.</p> <p>The code I am using to call it is in my main.cpp file:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;sstream&gt; #include "CommandParser.h" #include "CommandManager.h" #include "Exceptions.h" #include "Logger.h" using std::string; using std::stringstream; using std::cout; using std::cin; using std::endl; #define MSG_QUIT 2384321 #define SHOW_LOGGER true void RegisterCommands(void); void UnregisterCommands(void); int ApplicationLoop(void); void CheckForLoggingOutput(void); void ShowDebugLog(void); // Operations double Operation_Add(double* params); double Operation_Subtract(double* params); double Operation_Multiply(double* params); double Operation_Divide(double* params); // Variable CommandManager *mCommandManager; CommandParser *mCommandParser; Logger *mLogger; int main(int argc, const char **argv) { mLogger-&gt;WriteEntry("Registering commands...\0"); // Make sure we register all commands first RegisterCommands(); mLogger-&gt;WriteEntry("Command registration complete.\0"); // Check the input to see if we're using the program standalone, // or not if(argc == 0) { mLogger-&gt;WriteEntry("Starting application message pump...\0"); // Full version int result; do { result = ApplicationLoop(); } while(result != MSG_QUIT); } else { mLogger-&gt;WriteEntry("Starting standalone application...\0"); // Standalone - single use // Join the args into a string stringstream joinedStrings(argv[0]); for(int i = 1; i &lt; argc; i++) { joinedStrings &lt;&lt; argv[i]; } mLogger-&gt;WriteEntry("Parsing argument '" + joinedStrings.str() + "'...\0"); // Parse the string mCommandParser-&gt;Parse(joinedStrings.str()); // Get the command names from the parser list&lt;string&gt; commandNames = mCommandParser-&gt;GetCommandNames(); // Check that all of the commands have been registered for(list&lt;string&gt;::iterator it = commandNames.begin(); it != commandNames.end(); it++) { mLogger-&gt;WriteEntry("Checking command '" + *it + "' is registered...\0"); if(!mCommandManager-&gt;IsCommandRegistered(*it)) { // TODO: Throw exception mLogger-&gt;WriteEntry("Command '" + *it + "' has not been registered.\0"); } } // Get each command from the parser and use it's values // to invoke the relevant command from the manager double results[commandNames.size()]; int currentResultIndex = 0; for(list&lt;string&gt;::iterator name_iterator = commandNames.begin(); name_iterator != commandNames.end(); name_iterator++) { string paramString = mCommandParser-&gt;GetCommandValue(*name_iterator); list&lt;string&gt; paramStringArray = StringHelper::Split(paramString, ' '); double params[paramStringArray.size()]; int index = 0; for(list&lt;string&gt;::iterator param_iterator = paramStringArray.begin(); param_iterator != paramStringArray.end(); param_iterator++) { // Parse the current string to a double value params[index++] = atof(param_iterator-&gt;c_str()); } mLogger-&gt;WriteEntry("Invoking command '" + *name_iterator + "'...\0"); results[currentResultIndex++] = mCommandManager-&gt;InvokeCommand(*name_iterator, params); } // Output all results for(int i = 0; i &lt; commandNames.size(); i++) { cout &lt;&lt; "Result[" &lt;&lt; i &lt;&lt; "]: " &lt;&lt; results[i] &lt;&lt; endl; } } mLogger-&gt;WriteEntry("Unregistering commands...\0"); // Make sure we clear up our resources UnregisterCommands(); mLogger-&gt;WriteEntry("Command unregistration complete.\0"); if(SHOW_LOGGER) { CheckForLoggingOutput(); } system("PAUSE"); return 0; } void RegisterCommands() { mCommandManager = new CommandManager(); mCommandParser = new CommandParser(); mLogger = new Logger(); // Known commands mCommandManager-&gt;RegisterCommand("add", &amp;Operation_Add); mCommandManager-&gt;RegisterCommand("sub", &amp;Operation_Subtract); mCommandManager-&gt;RegisterCommand("mul", &amp;Operation_Multiply); mCommandManager-&gt;RegisterCommand("div", &amp;Operation_Divide); } void UnregisterCommands() { // Unregister each command mCommandManager-&gt;UnregisterCommand("add"); mCommandManager-&gt;UnregisterCommand("sub"); mCommandManager-&gt;UnregisterCommand("mul"); mCommandManager-&gt;UnregisterCommand("div"); // Delete the logger pointer delete mLogger; // Delete the command manager pointer delete mCommandManager; // Delete the command parser pointer delete mCommandParser; } int ApplicationLoop() { return MSG_QUIT; } void CheckForLoggingOutput() { char answer = 'n'; cout &lt;&lt; endl &lt;&lt; "Do you wish to view the debug log? [y/n]: "; cin &gt;&gt; answer; switch(answer) { case 'y': ShowDebugLog(); break; } } void ShowDebugLog() { mLogger-&gt;DisplayEntries(); } // Operation Definitions double Operation_Add(double* values) { double accumulator = 0.0; // Iterate over all values and accumulate them for(int i = 0; i &lt; (sizeof values) - 1; i++) { accumulator += values[i]; } // Return the result of the calculation return accumulator; } double Operation_Subtract(double* values) { double accumulator = 0.0; // Iterate over all values and negativel accumulate them for(int i = 0; i &lt; (sizeof values) - 1; i++) { accumulator -= values[i]; } // Return the result of the calculation return accumulator; } double Operation_Multiply(double* values) { double accumulator = 0.0; for(int i = 0; i &lt; (sizeof values) - 1; i++) { accumulator *= values[i]; } // Return the value of the calculation return accumulator; } double Operation_Divide(double* values) { double accumulator = 0.0; for(int i = 0; i &lt; (sizeof values) - 1; i++) { accumulator /= values[i]; } // Return the result of the calculation return accumulator; } </code></pre> <hr>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload