Optional Parameters in Functions

P

Programmer

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
 
V

Victor Bazarov

Programmer said:
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
 
A

Alan Sung

Victor Bazarov said:
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.


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top