std::string and const char[] conversions

S

sean345

While compiling my program I get the following g++ error:

error: no matching function for call to
`ErrorLog::file_error(std::string&, std::string&, const char[33])'
note: candidates are: void ErrorLog::file_error(const std::string&,
int, const std::string&)

The error line is:
log.file_error(filename, line, "variable is not assigned a value");

where filename is a std::string and line is an int. I do not
understand why the string literal will not convert to a const
std::string& as it does every other time I've used something like this.
This is the only error in the program and commenting out the line
above allows the program to compile successfully. Any suggestions?

- Sean
 
Z

Zara

While compiling my program I get the following g++ error:

error: no matching function for call to
`ErrorLog::file_error(std::string&, std::string&, const char[33])'
note: candidates are: void ErrorLog::file_error(const std::string&,
int, const std::string&)

The error line is:
log.file_error(filename, line, "variable is not assigned a value");

where filename is a std::string and line is an int. I do not
understand why the string literal will not convert to a const
std::string& as it does every other time I've used something like this.
This is the only error in the program and commenting out the line
above allows the program to compile successfully. Any suggestions?

The compiler is telling you that "line" is not an int, but a string.
Check if the "line" you want may be hidden by a local string line, or
something of the sort.

Regards,

Zara
 
A

Alf P. Steinbach

* (e-mail address removed):
While compiling my program I get the following g++ error:

error: no matching function for call to
`ErrorLog::file_error(std::string&, std::string&, const char[33])'
note: candidates are: void ErrorLog::file_error(const std::string&,
int, const std::string&)

The error line is:
log.file_error(filename, line, "variable is not assigned a value");

where filename is a std::string and line is an int. I do not
understand why the string literal will not convert to a const
std::string& as it does every other time I've used something like this.
This is the only error in the program and commenting out the line
above allows the program to compile successfully. Any suggestions?

I think you can trust the compiler regarding what the actual types are,
i.e., 'line' is not an int as you think it is: it is a std::string.

Perhaps you have overlooked some declaration.

Or perhaps you have some nasty macro called 'line' (it's a good idea to
only use UPPERCASE for macro names).
 
S

sean345

Alf said:
I think you can trust the compiler regarding what the actual types are,
i.e., 'line' is not an int as you think it is: it is a std::string.

Perhaps you have overlooked some declaration.

Or perhaps you have some nasty macro called 'line' (it's a good idea to
only use UPPERCASE for macro names).

Yes, you both are correct. It looks like line was an int, but in an
inner scope it was declared a string. I have a somewhat related
question. I am getting this error:

error: conversion from `const char[20]' to non-scalar type `Time'
requested

with this line:

Time t = "03/05/2006 11:17:51";

My Time class has both of these functions though:

Time(const std::string&);
const Time& operator=(const std::string&);

I would think that the line above calls the Time(const std::string&)
constructor, but it is obviously not. Any suggestions with this?

Thanks,
- Sean
 
A

Alf P. Steinbach

* (e-mail address removed):
Alf said:
I think you can trust the compiler regarding what the actual types are,
i.e., 'line' is not an int as you think it is: it is a std::string.

Perhaps you have overlooked some declaration.

Or perhaps you have some nasty macro called 'line' (it's a good idea to
only use UPPERCASE for macro names).

Yes, you both are correct. It looks like line was an int, but in an
inner scope it was declared a string. I have a somewhat related
question. I am getting this error:

error: conversion from `const char[20]' to non-scalar type `Time'
requested

with this line:

Time t = "03/05/2006 11:17:51";

My Time class has both of these functions though:

Time(const std::string&);
const Time& operator=(const std::string&);

I would think that the line above calls the Time(const std::string&)
constructor, but it is obviously not. Any suggestions with this?

The practical answer is to write

Time t( "03/05/2006 11:17:51" );

or

Time t = Time( "03/05/2006 11:17:51" );

or

Time t = std::string( "03/05/2006 11:17:51" );

or to add char const[] constructor to the time class.

But I find that if I try to explain the why (it's all about a
restriction to one user-defined conversion, so that the compiler won't
outsmart you by considering, say, a twelve-step conversion sequence), I
find that I get hopelessly lost -- it's the same with std::auto_ptr.

Instead of theory I just keep to simple practical solutions... ;-)
 
S

sean345

Alf said:
The practical answer is to write

Time t( "03/05/2006 11:17:51" );

or

Time t = Time( "03/05/2006 11:17:51" );

or

Time t = std::string( "03/05/2006 11:17:51" );

or to add char const[] constructor to the time class.

But I find that if I try to explain the why (it's all about a
restriction to one user-defined conversion, so that the compiler won't
outsmart you by considering, say, a twelve-step conversion sequence), I
find that I get hopelessly lost -- it's the same with std::auto_ptr.

Instead of theory I just keep to simple practical solutions... ;-)

Thanks for the explaination. I always assumed that the equal sign
constructor version was equivalent to the constructor function call
version.

- Sean
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top