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

std::string and const char[] conversions

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

Mar 7 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 6 Mar 2006 20:50:20 -0800, se*****@gmail.com wrote:
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
Mar 7 '06 #2

P: n/a
* se*****@gmail.com:
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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 7 '06 #3

P: n/a
Alf P. Steinbach wrote:
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

Mar 7 '06 #4

P: n/a
* se*****@gmail.com:
Alf P. Steinbach wrote:
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... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 7 '06 #5

P: n/a
Alf P. Steinbach wrote:
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

Mar 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.