Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 698161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:14:41+00:00 2026-05-14T03:14:41+00:00

I am trying to write a logger class for my C++ calculator, but I’m

  • 0

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

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.

* CODE ABOVE HAS BEEN ALTERED TO HOW IT IS NOW *

Now, the line:

cout << *it << endl;

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.

The code I am using to call it is in my main.cpp file:

#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;
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:14:41+00:00Added an answer on May 14, 2026 at 3:14 am

    Did you remember to call mLogger = new Logger at some point? Did you accidantally delete mLogger before writing to it?

    Try running your program in valgrind to see whether it finds any memory errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a small server using java.nio , but when trying to stress test
I'm utterly bamboozled by an app I'm trying to write which has suddenly stopped
In my PHP web application, I want to have the ability to perform the
I am trying to add a line to a text file with Java. When
I m working with NetBeans, and I m trying to program a program that
I am using JBoss5.1.x AS, EJB3.0. I am trying to add a job (using
I'm trying to create a program which will visualize different sorting algorithms by drawing
I am trying to call a method in a model from a template and
I need help, I am trying to make use of Lattice Multiplication in java
I am really new to CodeIgniter. I am trying to setup a website where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.