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

  • SEARCH
  • Home
  • 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 128029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:34:13+00:00 2026-05-11T05:34:13+00:00

Okay, so I have two classes, call them A and B–in that order in

  • 0

Okay, so I have two classes, call them A and B–in that order in the code. Class B instantiates class A as an array, and class B also has an error message char* variable, which class A must set in the event of an error. I created a third class with a pure virtual function to set the errorMessage variable in B, then made B a child of that third class. Class A creates a pointer to the third class, call it C–when B initializes the array of A objects, it loops through them and invokes a function in A to set A’s pointer to C– it passes ‘this’ to that function, and then A sets the pointer to C to ‘this,’ and since C is B’s parent, A can set C->errorMessage (I had to do all this because A and B couldn’t simultaneously be aware of each other at compile time).

Anyways it works fine, however, and when I pass command line parameters to main(int,char**), it works unless I pass seven, eight, or more than twelve parameters to it… I narrowed it down (through commenting out lines) to the line of code, in A, which sets the pointer to C, to the value passed to it by B. This made no sense to me… I suspected a memory leak or something, but it seems wrong and I have no idea how to fix it… Also I don’t get why specifically seven, eight, and more than twelve arguments don’t work, 1-6 and 9-12 work fine.

Here is my code (stripped down)–

//class C class errorContainer{  public:     virtual ~errorContainer(){ }     virtual void reportError(int,char*)=0;  };  //Class A class switchObject{     void reportError(int,char*);     errorContainer* errorReference;  public:     void bindErrorContainer(errorContainer*); };  //Class A member function definitions void switchObject::reportError(int errorCode,char* errorMessage){     errorReference->reportError(errorCode,errorMessage); }  void switchObject::bindErrorContainer(errorContainer* newReference){     errorReference=newReference; //commenting out this line fixes the problem }  //Class B class switchSystem: public errorContainer{     int errorCode;     char* errorMessage;  public:     switchSystem(int); //MUST specify number of switches in this system.     void reportError(int,char*);     int errCode();     char* errMessage();     switchObject* switchList; };  //Class B member function definitions switchSystem::switchSystem(int swLimit){     int i;     switchList=new (nothrow) switchObject[swLimit];     for(i=0;i<swLimit;i++){         switchList[i].bindErrorContainer(this);     }     errorCode=0;     errorMessage='No errors.'; }  void switchSystem::reportError(int reportErrorCode,char* reportErrorMessage){     int len=0,i;         errorCode=reportErrorCode;     if(errorMessage){         delete[] errorMessage;     }     while(reportErrorMessage[len]!='\0'){         len++;     }     errorMessage=new char[len];     for(i=0;i<=len;i++){         errorMessage[i]=reportErrorMessage[i];     }    }  int switchSystem::errCode(){     return errorCode;    }  char* switchSystem::errMessage(){     return errorMessage;     } 

Anyone know what I’ve done wrong here? It’s bugging the crap out of me… I can’t seem to fix it.

—EDIT— okay, I have it set up the way I do so that I can use it like this in main()

int main(int argc,char** argv){    switchSystem sw (2)    sw.switchList[0].argumentCount=2;    sw.switchList[1].argumentCount=0;    sw.switchList[0].identifier='a';    sw.switchList[1].identifier='switch';    sw.init(argc,argv);    if(sw.errCode()>0){       cout<< 'Error '<< sw.errCode()<< ': '<< sw.errMessage()<< endl;    } } 

this program is supposed to read the command line arguments and handle user defined ‘switches’–like how most command line programs handle switches, but instead of testing for all of them at the beginning of main I wanted to try to write a class and some functions to do it for me–create a switchSystem object with the number of switches, set their identifiers, whether or not they take arguments, and then pass the command line arguments to ‘init()’ to sort it out. Then test like,

if(sw.isSet('switch')){ ... }

etc.

  • 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. 2026-05-11T05:34:14+00:00Added an answer on May 11, 2026 at 5:34 am
    • reportError() should be declared virtual in switchSystem, as it is in errorContainer.
    • char* should instead be std::string to avoid all of that needless work.
    • Is there some reason that you can’t use an std::vector<switchObject> instead of new[]?
    • You shouldn’t delete[] errorMessage when it points to a static literal string. This leads to undefined behavior. (Translation: Bad Thing(TM).)
    • Why are you iteratively counting and copying the contents of a char*? This is begging for trouble. You’re not doing anything to protect yourself from harm.
    • Why must switchObject pass a string to switchSystem? Wouldn’t it be better to simply return an error code or throw some class derived from std::exception? Or perhaps it should send a string to a global logging facility?

    I think perhaps you should rethink your design instead of trying to fix this.

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

Sidebar

Related Questions

Okay, I have two sites, we'll call them A and B. Site A has
Okay I have two classes for two links and two divs with different information.
Okay, so we have a utility here in-house that generates business-model classes from our
EF Code First - Model I have 2 classes: public class Alias { public
Okay, I have two different background .jpgs that I want to used as the
Okay so I'm having a problem. I have two classes: ImageHandler and PixelHandler. In
If I have those two classes that have two different properties but with the
Okay so I have two import pieces of code involved in this. This first
Okay. I want to have two threads running. Current code: public void foo() {
Okay, so you have a load of methods sprinkled around your system's main class.

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.