I’m trying to add a insert a variable to a send().
here is the code:
string num;
// + num + is the reason for the error. Any work around or suggestions?
char *msg = "GET /index.php?num=" + num + " HTTP/1.1\nhost: domain.com\n\n";
int len;
ssize_t bytes_sent;
len = strlen(msg);
bytes_sent = send(socketfd, msg, len, 0);
I’m getting the error:
test.cpp: In function âint main()â:
test.cpp:64: error: cannot convert âstd::basic_string<char, std::char_traits<char>,
std::allocator<char> >â to âchar*â in initialization
–edit–
I tried to fix it with msg.c_str
cout << "send()ing message..." << endl;
string msg = "GET /index.php?num=" + num + " HTTP/1.1\nhost: domain.com\n\n";
int len;
ssize_t bytes_sent;
len = msg.lenght(); //updated to this and still gives me an error.
bytes_sent = send(socketfd, msg.c_str, len, 0);
Now it gives me the error:
error: argument of type âconst char* (std::basic_string<char, std::char_traits<char>,
std::allocator<char> >::)()constâ does not match âconst char*â
"stuff" + num + "more stuff"doesn’t do what you’d expect. Even if you were to convertstrto a char pointer, and even if C++ let you add char pointers together, it’d end up doing the totally wrong thing.(For reference, C++ doesn’t let you add pointers together, because the result doesn’t make any sense. Pointers are still just numbers, and adding two char pointers would basically amount to
0x59452448 + 0x10222250or something like that, which would return you a pointer to some location that probably doesn’t even exist yet…)Try this: