Problems calling system() in c++ [duplicate] - c++

This question already has answers here:
cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)'
(6 answers)
Closed 6 years ago.
I tried to use system() in a c++ app
it works very well if i do it like:
system("notepad");
But it gives error when i try to do like:
cin >> cmdlol;
system(cmdlol);
Error:
cannot convert 'std::string {aka std::basic_string}' to 'const
char*' for argument '1' to 'int system(const char*)'|

cmdlol seemes to be std::string, which can't be converted to const char* implicitly. And std::system only accepts const char* as its argument, that's why compiler complains.
You could use std::basic_string::c_str() explicitly.
system(cmdlol.c_str());
And about why system("notepad"); works well, "notepad" is a string literal with type const char[8] (including null character), note it's not std::string and might decay to const char* when passed to std::system.

std::system wants a const char*, that's a C-style array.
To convert a std::string to a const char*, you can use the function c_str().
system(cmdlol.c_str());

Related

Change a C library function to get rid of implicit const cast warnings from g++ [duplicate]

This question already has answers here:
Why is conversion from string constant to 'char*' valid in C but invalid in C++
(4 answers)
Closed 3 years ago.
very simple code does warn me. Some hints are not constructive. Warning is:
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
I tried:
char const *q = "pin";
char const *r = "\n\r";
{
while(client.findUntil(*q, *r))
without success
Origin code:
while(client.findUntil("pin", "\n\r"))
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
while(client.findUntil("pin", "\n\r"))
The warning means that your program is ill-formed. You didn't show the declaration, but from context, we can deduce that the argument of findUntil is char*. You may not pass a string literal to such function.
It used to be well-formed - but deprecated - to pass a string literal as char* prior to C++11.
I tried:
char const *q = "pin";
char const *r = "\n\r";
These are correct by themselves, but
while(client.findUntil(*q, *r))
This makes no sense. Previously your were attempting to pass a string, but now you indirect through the character pointer so you are passing a character. Unless the function is a template, this cannot possibly work.
findUntil(q, r) won't work either, because the pointers to const won't implicitly convert to pointers to non-const.
A correct solution is to copy the string literals into modifiable arrays:
char q[] = "pin";
char r[] = "\n\r";
{
while(client.findUntil(q, r))
Another is to fix findUntil to accept a pointer to const char instead. Then you can use string literals, since they can be converted to a pointer to const char.

Is possible to convert argument from 'const char []' to 'char *' in C++?

Definition of method is:
void setArgument(char *);
And i call that method with this code:
setArgument("argument");
But my VisualStudio compiler gets me the next error:
cannot convert argument 1 from 'const char [10]' to 'char *'
Is it possible to send arguments like this or I must change arguments type in the method?
Also, VS show me next note in output: note: Conversion from string literal loses const qualifier (see /Zc:strictStrings)
The problem is that string literals are arrays of constant characters.
While the array could easily decay to a pointer to its first element, the type of that pointer is const char *. Which needs to be the type of the argument for your functions.
And if you need to modify the string you pass, then you should create your own non-constant array:
char argument[] = "argument";
setArgument(argument);
Of course, since you're programming in C++ you should stop using char pointers and arrays and instead use std::string.
It's possible, just if you really need the argument to be mutable (char* and not char const*), you need to allocate a new storage in the mutable memory and clone the contents of the constant memory to there, if that fits into your definition of "convert".
auto const len = strlen(input);
auto const buf = std::unique_ptr<char[]>(new char[len + 1]);
memcpy(buf, input, len + 1);
If you actually need char const* and if you are C++17 or later, you can possibly change the signature to setArgument(std::string_view arg), making it misuse-proof.

Can't (implicitly) convert from char** to const char**? [duplicate]

This question already has answers here:
Cannot convert from XXX** to const XXX** [duplicate]
(3 answers)
How to convert "pointer to pointer type" to const?
(2 answers)
Closed 4 years ago.
E.g. the following will fail to compile:
auto foo = [](const char**) {};
char* ptr = nullptr;
foo(&ptr);
Visual studio says that there's a conversion that loses qualifiers. Clang says cannot initialize a parameter of type 'const char **' with an rvalue of type 'char **". Perhaps my brain just isn't working today, but why is this dis-allowed? char* can implicitly convert to const char* after all
Oh wait, yes my brain isn't working today. foo can assign a const char* to the pointer, which is clearly bad.

Invalid operands of types ‘const char [8]’ and ‘const char*’ to binary ‘operator+

I am trying to write a Fopen statement like this:
FILE *fp;
fp = fopen("client." + receiver->get_identifier().c_str() + ".vol", "a+");
where receiver->get_identifier() returns a string. However, I am getting the error in the title. I read the question here but wasn't having any luck because the first argument of fopen is const char*. What do I have to change to get this to compile?
receiver->get_identifier().c_str()
returns a const char*, not a std::string, so operator+ cannot kick in (one argument of it must be a std::string). Removing the c_str() and converting at the end with std::string::c_str() should do the trick
fopen(("client." + receiver->get_identifier() + ".vol").c_str(), "a+");
This is because you'd have a const char* plus a std::string, and operator+ will work.
If you may wonder why one cannot define an operator+ for const char*s, that's because C++ does not allow operator overloading for fundamental types; at least one argument must be of a user-defined type.
Try changing the first argument to
(string("client.") + receiver->get_identifier() + ".vol").c_str()
This will add std::string objects with C-Style strings, which can be done, and only take the character pointer at the end (via .c_str()). Your code now tries to add C-Style strings, which is not possible.

c++ const convert [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const)
I have a function:
bool isCirclePolygonIntersection(const Point*, const int*, const Point*,
const Point**, const int*);
and I'm trying to call it like this:
isCirclePolygonIntersection(p, &r, poly_coord, poly, &poly_size)
where poly defined like this:
Point** poly = new Point*[poly_size];
and there is a compiler error when I'm trying to compile it:
error C2664: 'isCirclePolygonIntersection' : cannot convert parameter 4 from 'Point **' to 'const Point **'
1> Conversion loses qualifiers
from what I've learned is that you cannot give const argument to function when a function expects a non const argument, but it's fine otherwise.
Does anyone knows what is the problem??
Thanks.
You're correct; you can implicitly convert a T * to a const T *. However, you cannot implicitly convert a T ** to a const T **. See this from the C FAQ: http://c-faq.com/ansi/constmismatch.html.
An implicit conversion from Point** to const Point** would open a hole in the type system, hence there is no such conversion in C++.
For details, see Why am I getting an error converting a Foo** → Foo const**?
Changing const Point** to const Point * const * will fix your problem.
By the way, why the additional indirection? That is, why do you use an array of pointers? I would suggest a std::vector<Point> instead. Or is Point a polymorphic base class?
Parashift has a great explanation on why this is prohibited:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17