473,412 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

std::string and const char[] conversions

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
5 5393
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
* 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
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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
3
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char,...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.