Optional Parameters in Functions

Discussion in 'C++' started by Programmer, Aug 20, 2003.

  1. Programmer

    Programmer Guest

    This may sound easy and it probably is, I just started learning C++.

    I have a class that writes to a file and I would like to add a second
    optional parameter that will be used to tell the class if a newline
    character should be written so the next output will start on a new line.

    My class looks like this:
    class Debug {
    public:
    Debug();
    void Open(string);
    void Write(string,bool);
    void Write(float,bool);
    void Write(int,bool);
    void Write(double,bool);
    void Write(char,bool);
    ~Debug();

    private:
    ofstream outDebug;
    };

    And my definition of one of these looks like this:
    void Debug::Write(string sDebug, bool bNewLine = true) {

    if(bNewLine) {
    outDebug << sDebug << endl;
    } else {
    outDebug << sDebug;
    }
    }

    I want to set it up so that by default, it will write a new line, but it can
    be overridden by sending a 'false' as the second parameter. However, I am
    getting a "Write: no overloaded function takes 1 parameters" error when I
    try to compile. The call looks like this:

    DebugFile.Write("Debug File Opened");

    I could write it like this, DebugFile.Write("Debug File Opened",true), but
    since true is the default and the mode I will use 90% of the time, I was
    hoping to be able to default to true so my calls will be shorter.

    Any ideas?

    Thanks in advance

    John
    Programmer, Aug 20, 2003
    #1
    1. Advertising

  2. "Programmer" <> wrote...
    > This may sound easy and it probably is, I just started learning C++.
    >
    > I have a class that writes to a file and I would like to add a second
    > optional parameter that will be used to tell the class if a newline
    > character should be written so the next output will start on a new line.
    >
    > My class looks like this:
    > class Debug {
    > public:
    > Debug();
    > void Open(string);
    > void Write(string,bool);


    Change this to

    void Write(string, bool = true);

    > void Write(float,bool);
    > void Write(int,bool);
    > void Write(double,bool);
    > void Write(char,bool);
    > ~Debug();
    >
    > private:
    > ofstream outDebug;
    > };
    >
    > And my definition of one of these looks like this:
    > void Debug::Write(string sDebug, bool bNewLine = true) {


    The default argument values should be used in the declaration,
    not the definition. Remove it from here. Leave this as

    void Debug::Write(string sDebug, bool bNewLine) {

    .. Also, passing a string by value is inefficient. Passing it by
    a reference to const is better.

    >
    > if(bNewLine) {
    > outDebug << sDebug << endl;
    > } else {
    > outDebug << sDebug;
    > }
    > }
    >
    > I want to set it up so that by default, it will write a new line, but it

    can
    > be overridden by sending a 'false' as the second parameter. However, I am
    > getting a "Write: no overloaded function takes 1 parameters" error when I
    > try to compile. The call looks like this:
    >
    > DebugFile.Write("Debug File Opened");
    >
    > I could write it like this, DebugFile.Write("Debug File Opened",true), but
    > since true is the default and the mode I will use 90% of the time, I was
    > hoping to be able to default to true so my calls will be shorter.
    >
    > Any ideas?


    See above.

    Victor
    Victor Bazarov, Aug 20, 2003
    #2
    1. Advertising

  3. Programmer

    Alan Sung Guest

    "Victor Bazarov" <> wrote in message
    news:K0C0b.204068$Ho3.27244@sccrnsc03...
    > "Programmer" <> wrote...
    > > And my definition of one of these looks like this:
    > > void Debug::Write(string sDebug, bool bNewLine = true) {

    >
    > The default argument values should be used in the declaration,
    > not the definition. Remove it from here. Leave this as
    >
    > void Debug::Write(string sDebug, bool bNewLine) {
    >
    > . Also, passing a string by value is inefficient. Passing it by
    > a reference to const is better.
    >
    > >
    > > if(bNewLine) {
    > > outDebug << sDebug << endl;
    > > } else {
    > > outDebug << sDebug;
    > > }
    > > }
    > >
    > > I want to set it up so that by default, it will write a new line, but it

    > can
    > > be overridden by sending a 'false' as the second parameter. However, I

    am
    > > getting a "Write: no overloaded function takes 1 parameters" error when

    I
    > > try to compile. The call looks like this:
    > >
    > > DebugFile.Write("Debug File Opened");
    > >
    > > I could write it like this, DebugFile.Write("Debug File Opened",true),

    but
    > > since true is the default and the mode I will use 90% of the time, I was
    > > hoping to be able to default to true so my calls will be shorter.
    > >
    > > Any ideas?

    >
    > See above.
    >
    > Victor


    I should also add:

    void Write(const char *,bool=true);
    in order to handle quoted strings so that you don't have to construct a
    temporary string.

    -al sung
    Rapid Realm Technology, Inc.
    Hopkinton, MA
    Alan Sung, Aug 20, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Do
    Replies:
    1
    Views:
    67,119
    Kevin Spencer
    Oct 16, 2003
  2. Hanover

    SQLCommand with Optional Parameters?

    Hanover, Sep 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,443
    rviray
    Sep 28, 2005
  3. Joerg Toellner
    Replies:
    4
    Views:
    1,121
    Joerg Toellner
    Jul 16, 2003
  4. Replies:
    1
    Views:
    531
    Chris \( Val \)
    Dec 9, 2003
  5. Bo Peng
    Replies:
    7
    Views:
    423
    tom_usenet
    Jul 7, 2004
Loading...

Share This Page