Default value for a string

Hello guys,

I need to have a default value for a string in a function parameter, but the function call keeps complaining "no matching function for call..." when called with no parameters. I'm trying to do this with different ways:

1
2
3
void writeLog(string file = string("Error.log")) throw()
void writeLog(string file = "Error.log") throw()
void writeLog(char* file = "Error.log") throw()


All of them have failed.
Note: I don't want the type to be const.

Could you guys help me with this?

Thank you in advance :-)
I got it! I just had to put the default value in the class prototype!!! but I don't understand why... could anyone explain?

EDIT: I mean not in the definition of the function, but in the prototype in the class.
Last edited on
Could you show the entire error message? Do you have the writeLog() function defined before you are calling it?
I think we would need to see more code to tell. This code works for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void writeLog(std::string file = "Error.log") throw();

int main()
{
	writeLog();
}

void writeLog(std::string file) throw()
{
	std::cout << file << '\n';
}
Error.log

However I would probably pass a const reference like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void writeLog(const std::string& file = "Error.log") throw();

int main()
{
	writeLog();
}

void writeLog(const std::string& file) throw()
{
	std::cout << file << '\n';
}
Error.log
Last edited on
OK, here's the code. This way it has worked fine. But if I put the default value in the function definition part the compiler doesn't accept it.

So the prototype is:
 
virtual void writeLog(string file = "Error.log") throw();


definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void Exception_sam_base::writeLog(string file) throw()
{
    fstream errorlogfile(file.c_str(), ios::in | ios::out | ios::app);

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    errorlogfile.seekp(0,ios::end);

    errorlogfile << "Program Name: "        << (ProgramName.length() != 0 ? ProgramName : "Program name is undefined. Set the string Exception_sam_base::ProgramName to define it.") << endl;
    errorlogfile << "Date: "                << asctime(timeinfo)    << endl;
    //errorlogfile << "Error ID: "            << ID                   << endl;
    errorlogfile << "Error throw: "         << ErrorMessage         << endl;
    errorlogfile << "Error description: "   << Details              << endl;
    errorlogfile << "Possible resolution: " << Resolution           << endl;
    errorlogfile << "--------------------" << endl;
    errorlogfile << "--------------------" << endl;
    errorlogfile << "Variables at exception: ";
    if (vars.size() == 0)
        errorlogfile << "None.";
    else
    {
        errorlogfile << endl;
        for (int i = 0; i < vars.size(); i++)
        {
            for (int j = 0; j < vars[i].size(); j++)
            {
                errorlogfile << vars[i][j] << "\t";
            }
            errorlogfile << endl;
        }
    }
    errorlogfile.close();
}


So I don't get it, why must we put the default value at the prototype? any explanation please?
Default values are a feature of the prototype, not the implementation. They are merely 'syntactic sugar' that the compiler uses to provide parameters at compile time that the programmer omitted to specify. They are not part of the function definition itself.
But I have another template class where I used default values in the implementation and not in the prototypes!!! that's why I'm surprised. (Since it's a template class, it has everything in a single file, not in 2 files like in this case. Does this have anything to do with that?)
ehm??? come on :(
TheDestroyer wrote:
But I have another template class where I used default values in the implementation and not in the prototypes!!!
a templated class/function is actually a prototype which you instantiate later.

having a default parameter in the implementation doesn't make any sense since it's totally veilled from the user of that function. If you had something like that
1
2
3
4
5
6
7
void func(int i, int j); // prototype

...

void func(int i, int j = 0) // the user of func() must provide 'j', what's the default parameter good for?
{
}
I see! thank you so much :)
Topic archived. No new replies allowed.