Converting std::__cxx11::string to std::string
Is it possible that you are using GCC 5?
If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.
Source: GCC 5 Release Notes/Dual ABI
Defining the following macro before including any standard library headers should fix your problem: #define _GLIBCXX_USE_CXX11_ABI 0
Getting error: cannot convert 'std::__cxx11::string* {aka std::__cxx11::basic_stringchar*}' to 'const char*' error while passing a path as argument
Use the string::c_str()
method, like stat(path.c_str())
, to convert a C++ string to a C string.
See http://cplusplus.com/reference/string/string/c_str/ for more information.
error: could not convert from 'std::string* {aka std::basic_stringchar*}' to 'std::string {aka std::basic_stringchar}'|
An array of string
items is passed as actual argument, where the formal argument is a single string
.
You could replace the array with a single string.
Or with a set, or whatever suits the purpose, but then you'll have to change the called function accordingly.
The error mentions std::string*
instead of an array, like std::string[9]
, because the array expression decays to an expression denoting a pointer to the first item, which it does because the array is not being bound to a reference or passed to sizeof
or anything where the expression's array nature would have to be preserved.
How to convert a std::string to const char* or char*
If you just want to pass a std::string
to a function that needs const char*
you can use
std::string str;
const char * c = str.c_str();
If you want to get a writable copy, like char *
, you can do that with this:
std::string str;
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0'; // don't forget the terminating 0
// don't forget to free the string after finished using it
delete[] writable;
Edit: Notice that the above is not exception safe. If anything between the new
call and the delete
call throws, you will leak memory, as nothing will call delete
for you automatically. There are two immediate ways to solve this.
boost::scoped_array
boost::scoped_array
will delete the memory for you upon going out of scope:
std::string str;
boost::scoped_array<char> writable(new char[str.size() + 1]);
std::copy(str.begin(), str.end(), writable.get());
writable[str.size()] = '\0'; // don't forget the terminating 0
// get the char* using writable.get()
// memory is automatically freed if the smart pointer goes
// out of scope
std::vector
This is the standard way (does not require any external library). You use std::vector
, which completely manages the memory for you.
std::string str;
std::vector<char> writable(str.begin(), str.end());
writable.push_back('\0');
// get the char* using &writable[0] or &*writable.begin()
cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_stringchar}' to 'LPCSTR {aka const char*}'
Do you use Visual Studio?
If your project's Character Set
is Use Unicode Character Set
in the Project's Property window, MoveFile means that MoveFileW.
It's parameter type is LPCTSTR, that is const wchar_t *, not the const char *.
Your error is not in converting from string to const char *, just in parameter type error in MoveFile.
You can fix this by use MoveFileA, MoveFileA(OldPNGFolder.c_str(), NewPNGFolder.c_str());
or by convert string to wstring or LPCWSTR as below.
wstring a2w(std::string & string_a)
{
int length = MultiByteToWideChar(CP_UTF8, 0, string_a.c_str(), -1, NULL, 0);
wchar_t* temp = new wchar_t[length];
MultiByteToWideChar(CP_UTF8, 0, string_a.c_str(), -1, temp, length);
wstring string_w = temp;
delete[] temp;
return string_w;
}
int main() {
...
MoveFile(a2w(OldPNGFolder).c_str(), a2w(NewPNGFolder).c_str());
}
Related Topics
How to "Return an Object" in C++
How to Have Functions Inside Functions in C++
Remove Spaces from Std::String in C++
Passing Object by Reference to Std::Thread in C++11
What Happens If You Static_Cast Invalid Value to Enum Class
How to Create an Std::Function from a Move-Capturing Lambda Expression
Declaring Variables Inside Loops, Good Practice or Bad Practice
What Is Meant With "Const" At End of Function Declaration
"Undefined Reference To" Template Class Constructor
Modern Way to Set Compiler Flags in Cross-Platform Cmake Project
Finding C++ Static Initialization Order Problems
Is the Pimpl Idiom Really Used in Practice
Developing C Wrapper API For Object-Oriented C++ Code
Catching Exception: Divide by Zero
What Is a Converting Constructor in C++ ? What Is It For
When Do I Use a Dot, Arrow, or Double Colon to Refer to Members of a Class in C++