Cannot Convert 'Std::Basic_String<Char>' to 'Const Char*' for Argument '1' to 'Int System(Const Char*)'

cannot convert 'std::basic_string char ' to 'const char*' for argument '1' to 'int system(const char*)'

The type of expression

" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'"

is std::string. However function system has declaration

int system(const char *s);

that is it accepts an argumnet of type const char *

There is no conversion operator that would convert implicitly an object of type std::string to object of type const char *.

Nevertheless class std::string has two functions that do this conversion explicitly. They are c_str() and data() (the last can be used only with compiler that supports C++11)

So you can write

string name = "john";

system( (" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'").c_str() );

There is no need to use an intermediate variable for the expression.

cannot convert 'std::basic_string char ' to 'const char*' for argument '1' to 'int system(const char*)'

The type of expression

" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'"

is std::string. However function system has declaration

int system(const char *s);

that is it accepts an argumnet of type const char *

There is no conversion operator that would convert implicitly an object of type std::string to object of type const char *.

Nevertheless class std::string has two functions that do this conversion explicitly. They are c_str() and data() (the last can be used only with compiler that supports C++11)

So you can write

string name = "john";

system( (" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'").c_str() );

There is no need to use an intermediate variable for the expression.

C++ error: cannot convert ‘std::basic_string char ’ to ‘const char*’

C-strings can't be concatenated with +.

Use std::string::+ instead:

downloadFile((test + "filename.txt").c_str(), "/user/tmp/file.txt");

Note that c_str only returns a pointer to the std::string's internal character array, so it's valid only during the execution of the downloadFile function.

c++ error: cannot convert basic_string char }' to 'const char*' for argument '1' to 'long int strtol

You can use strtol(str[j].c_str(), &p, 10); the call to c_str() returns a const char* that points at the contents of the string object, and strtol wants a const char*. Or you can write more idiomatic code, and call std::stol(str[j]).

cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'int system(const char*)

Use the c_str() method:

system(fold.c_str());

error: cannot convert 'std::basic_string char ::iterator ...' to 'const char* for argument '1' ...'

You forgot to #include <algorithm>, where std::remove is located. Without that, your compiler only knows about this std::remove (I get the same error with Visual C++ 14), which is defined in indirectly included <cstdio> header.

Different behavior among compilers is a result of different #include hierarchies of the standard library implementations.

.cpp:23: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’

Use stoi, it's the modern C++ version of C's atoi.


Update:

Since the original answer text above the question was amended with the following error message:

‘stoi’ was not declared in this scope

Assuming this error was produced by g++ (which uses that wording), this can have two different causes:

  • Using a non-conforming variant of g++ that doesn't provide std::stoi.

  • Using g++ in C++03 mode (stoi was introduced in C++11).

For Windows, the MinGW-w64 variant is known to provide std::stoi, and in particular the Nuwen distribution is based on MinGW-w64.

For C++11 mode, with g++ use the option -std=c++11. For example, this is necessary with the Nuwen distribution g++ version 5.1.

cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string char }' 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());
}

cannot convert 'std::string {aka std::basic_string char }' to 'char*' for argument '2' to 'int Save(int, char*)'

As the error message says, you're trying to pass a std::string to a function that expects a pointer to a character array.

The best solution is to change the function to work with a string:

int Save (int key_stroke, const std::string & file);

and then extract a pointer when you need one

fopen(file.c_str(), "a+");
^^^^^^^^

Alternatively, if you want to preserve the C-style aesthetic of your code, you could change the parameter type to const char *, and pass fileN.c_str() to it.

c++ cannot convert ‘std::string’ {aka ‘std::__cxx11::basic_string’} to ‘std::string (*)[3]’ {aka ‘std::__cxx11::basic_string (*)[3]’}

The problem is that the type of the expression Board[3][3] when passing as a call argument is std::string while the type of the function parameter GameBoard is actually ‘std::string (*)[3]. This is because of string [3][3] decays to string (*)[3] due to type decay.

Thus there is mismatch in the type of the parameter of the function HasWon and the argument passed.

Also note that in standard c++, the size of an array must be a compile time constant. So in you code:

int Length = 3;
string Board[Length][Length]; //this is not standard C++

The statement string Board[Length][Length]; is not standard C++ because Length is not a constant expression.

To solve this you should add a top level const to Length and pass the argument Board instead of Board[3][3] as shown below:

//-vvvvv---------------------------> const added here
const int Length = 3;
//----------------vvvvv------------> changed to Board instead of Board[3][3]
cout << HasWon(Board, Length);

Demo.

Another alternative(better) would be to use std::vector.



Related Topics



Leave a reply



Submit