?
Solved

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

Posted on 2016-10-01
6
Medium Priority
?
546 Views
Last Modified: 2016-10-03
Hi

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.


A.h
#include <string>

public:
A(const char* filename);

private:
void  AnotherFunc(const char* filename);


---------------------

A.cpp
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?
0
Comment
Question by:LuckyLucks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 41824902
Ate you sure that code represents the code generating the error? You've not used std::string anywhere in it that I can see.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 41824903
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.
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 500 total points
ID: 41824972
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Accepted Solution

by:
evilrix earned 1000 total points
ID: 41825001
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.
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 500 total points
ID: 41825007
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.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 500 total points
ID: 41825016
>> 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.
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question