gpt4 book ai didi

c++ - 尝试将字符串推到列表后面时出现段错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:24:16 26 4
gpt4 key购买 nike

我正在尝试为我的 C++ 计算器编写一个记录器类,但在尝试将字符串插入列表时遇到问题。

我已尝试研究此问题并找到了一些相关信息,但似乎对解决我的问题没有任何帮助。我使用的是一个相当基本的 C++ 编译器,几乎没有调试实用程序,而且我已经有一段时间没有使用 C++(即使当时也只是很少用)。

我的代码:

#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() {}
~Logger() {}

// Public Methods
void WriteEntry(const string& entry)
{
mEntries.push_back(entry);
}

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

for(list<string>::iterator it = mEntries.begin();
it != mEntries.end(); it++)
{
// *** BELOW LINE IS MARKED WITH THE ERROR ***
cout << *it << endl;
}
}
};

#endif

我通过简单地传入一个字符串来调用 WriteEntry 方法,如下所示:

mLogger->WriteEntry("Testing");

如有任何建议,我们将不胜感激。

* 上面的代码已经更改为现在的样子 *

现在,行:

cout << *it << endl;

导致同样的错误。我假设这与我尝试从迭代器获取字符串值的方式有关。

我用来调用它的代码在我的 main.cpp 文件中:

#include <iostream>
#include <string>
#include <sstream>
#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->WriteEntry("Registering commands...\0");

// Make sure we register all commands first
RegisterCommands();

mLogger->WriteEntry("Command registration complete.\0");

// Check the input to see if we're using the program standalone,
// or not
if(argc == 0)
{
mLogger->WriteEntry("Starting application message pump...\0");

// Full version
int result;
do
{
result = ApplicationLoop();
} while(result != MSG_QUIT);
}
else
{
mLogger->WriteEntry("Starting standalone application...\0");

// Standalone - single use
// Join the args into a string
stringstream joinedStrings(argv[0]);
for(int i = 1; i < argc; i++)
{
joinedStrings << argv[i];
}

mLogger->WriteEntry("Parsing argument '" + joinedStrings.str() + "'...\0");

// Parse the string
mCommandParser->Parse(joinedStrings.str());

// Get the command names from the parser
list<string> commandNames = mCommandParser->GetCommandNames();

// Check that all of the commands have been registered
for(list<string>::iterator it = commandNames.begin();
it != commandNames.end(); it++)
{
mLogger->WriteEntry("Checking command '" + *it + "' is registered...\0");

if(!mCommandManager->IsCommandRegistered(*it))
{
// TODO: Throw exception
mLogger->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<string>::iterator name_iterator = commandNames.begin();
name_iterator != commandNames.end(); name_iterator++)
{
string paramString = mCommandParser->GetCommandValue(*name_iterator);
list<string> paramStringArray = StringHelper::Split(paramString, ' ');

double params[paramStringArray.size()];
int index = 0;
for(list<string>::iterator param_iterator = paramStringArray.begin();
param_iterator != paramStringArray.end(); param_iterator++)
{
// Parse the current string to a double value
params[index++] = atof(param_iterator->c_str());
}

mLogger->WriteEntry("Invoking command '" + *name_iterator + "'...\0");

results[currentResultIndex++] =
mCommandManager->InvokeCommand(*name_iterator, params);
}

// Output all results
for(int i = 0; i < commandNames.size(); i++)
{
cout << "Result[" << i << "]: " << results[i] << endl;
}
}

mLogger->WriteEntry("Unregistering commands...\0");

// Make sure we clear up our resources
UnregisterCommands();

mLogger->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->RegisterCommand("add", &Operation_Add);
mCommandManager->RegisterCommand("sub", &Operation_Subtract);
mCommandManager->RegisterCommand("mul", &Operation_Multiply);
mCommandManager->RegisterCommand("div", &Operation_Divide);
}

void UnregisterCommands()
{
// Unregister each command
mCommandManager->UnregisterCommand("add");
mCommandManager->UnregisterCommand("sub");
mCommandManager->UnregisterCommand("mul");
mCommandManager->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 << endl << "Do you wish to view the debug log? [y/n]: ";
cin >> answer;

switch(answer)
{
case 'y':
ShowDebugLog();
break;
}
}

void ShowDebugLog()
{
mLogger->DisplayEntries();
}

// Operation Definitions
double Operation_Add(double* values)
{
double accumulator = 0.0;

// Iterate over all values and accumulate them
for(int i = 0; i < (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 < (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 < (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 < (sizeof values) - 1; i++)
{
accumulator /= values[i];
}

// Return the result of the calculation
return accumulator;
}

最佳答案

你记得在某个时候调用 mLogger = new Logger 吗?您是否在写入之前不小心删除了 mLogger

尝试在 valgrind 中运行您的程序,看它是否发现任何内存错误。

关于c++ - 尝试将字符串推到列表后面时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2569524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com