• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 857
  • Last Modified:

C++ error cannot convert from std::string to const char *


I am getting a cannot convert from 'std::string' to 'const char*' error error 2664 inside a constructor.

I haven't called the method yet from anywhere.  I have just set up the class .h and .cpp and I am running into this.  Note: i have #include <string> in the header file for the class.

#include <string>

A(const char* filename);

void  AnotherFunc(const char* filename);


A::A(const char* filename){
   AnotherFunc(filename);            //line flagged

A::AnotherFunc(const char* filename){

I get an error at the line flagged , saying it cannot convert from std::string to const  char *. ideas why?
  • 3
  • 2
4 Solutions
evilrixSenior Software Engineer (Avast)Commented:
Ate you sure that code represents the code generating the error? You've not used std::string anywhere in it that I can see.
evilrixSenior Software Engineer (Avast)Commented:
If you are trying to construct A with a std::string, you can't. There is no implicit conversion from a string to a char pointer, const or otherwise.
It is more usual and more recommended not to use const char* arguments in new code.

In the past years, it was better to use const std::string& instead. In the latest years, the trend is even more radical, but I am not that experienced to talk about that.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

evilrixSenior Software Engineer (Avast)Commented:
I don't disagree with pepr; however, if you are only passing around a char pointer you have to be aware that this would involve the implicite construction of a temporary std::string, which is suboptimal for repeated calls. C++ is notorious for this and so you do need to be careful. Each temporary string involves a heap allocation internally (although some implementations of string to carry a small static buffer for small string optimisation. The point is, try and be consistent with your types and be aware of creating unnecessary temporaries.
To add to evilrix's comment... My recommendation is to switch to using std::string also outside he called methods. This way, passing the argument via a reference will not cause creating a temporary std::string object. The only exception would be cases when you really need to pass the char* pointer that you get somehow else (third party, not in your hands). Or, it will be the case when you pass a string literal.
>> If you are trying to construct A with a std::string, you can't.
Yeah, author's code doesn't show that, but that is what would give the error: A(std::string filename);

You could modify the constructor body:
A::A(std::string filename)
	AnotherFunc( filename.c_str() );

Open in new window

The const char * is now consistent in AnotherFunc().
If you are going with std::string, why not stick with it in your class.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now