By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Optional Parameters in Functions

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Programmer" <js******@jsjholdings.com> 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
Jul 19 '05 #2

P: n/a
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:K0C0b.204068$Ho3.27244@sccrnsc03...
"Programmer" <js******@jsjholdings.com> 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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.