cannot convert ‘std::basic_string<char, std::char_traits<char>...etc

Hi ev'ry one! :)

It's my first post there, i'm a new member, i'm a beginner in C++, and i'm french (so please be patient if i don't understand well and fast).

I'm trying to do this (see the code) but i have this error that i don't understand:

cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘const char*’ for argument ‘1’ to ‘int system(const char*)


Can you help me please? Thanks!!

My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <stdlib.h>

using namespace std;

int main() {
    
    string url;
    cout << "Type url to ping: ";
    cin >> url;

    system("ping " + url);
    
}
You can't just add const char* to a string. This might work for you:

1
2
3
4
5
6
7
8
string command = "ping ";
string url;
cout << "Type url to ping: ";
cin >> url;

command.append(url);

system(command.c_str());
All right, thank you Galik, it works like that! :)

But i don't understand what is the "const char*"...
const char * is a constant character pointer to a character stream.

It's widely used in replacement of string functions by libraries and other assistance based functions.

for example:

 
const char * string = "This works!";
1
2
string str = "Test";
const char *pStr = str.c_str();
Last edited on
All right, thank you friends! i'm gonna look to know more about this.

However i do not understand why there is "const" in this declaration... May be it's too technical for my beginner brain... but i'll understand, some day... :)
When you put a 'string literal' in your code like this "some text" the compiler actually returns the place in memory where that text is stored. That place in memory is an 'address of' or 'pointer to' the text itself. So it is a char*. But more than that, the text itself is stored in an area of memory that can not be changed. So it is typed as const.

So the 'type' of a string literal is a const char* (a pointer to an array of constant characters).
Last edited on
Thank you Galik, it's luminous told like that. I understand better the role of pointer... I think i do understand, i hope i do :)

But why does it takes this to be a constant?
because it is.
think about it: it's typed into the source code, in "quotes", embedded in
at compile time, all wrapped up in the executable file.
If you open the executable in an editor you'll see it there.
so It is constant. You wouldn't want to write to the program,
in normal civilised company while running would you?
It would probably blow up.


So why do not say to the compiler at compile time that it's a variable?

I think now that i have to understand better what compilation is.
well, you haven't named a variable.
if it's in quotes it's a const char * as explained.
if you assign it to a string it will be copied.
that string of characters will still exist in the binary file.

you need to learn about "the stack" and automatic variables,
as a starting point.

I am afraid you have a lot to learn!
but it is all good fun.






Last edited on
You are afraid? then i am more afraid than i was before :S

Well, let's take it because it's fun, and i'm going to seek for the knowledge of "the stack" and automatic variables :)

i hope i'll get it...
Constness only indicates that you pledge not to modify a specific item... this is good when you have objects in memory that should not be modified (except in special circumstances), or cannot be modified (because you or your program don't have that privilege). Making a thing const is useful because the compiler can check to make sure you aren't trying to modify things you aren't supposed to modify...

Here is a silly example, and a small discussion on pointers:
http://www.cplusplus.com/forum/beginner/19479/#msg101113

Notice how if the replacement string were "Really, really Stupid", then the program would be modifying itself in places where it shouldn't (outside of the space allocated to store the "Stupider" string -- and probably somewhere in the program's code).

Hope this helps.
Thank you Duoas! and i thank all of you that answered to me! :)
Topic archived. No new replies allowed.