2

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*â
4
  • 1
    There's a reason they made a string class and don't use char pointers and arrays directly anymore -- which is, stuff like chars + string + chars doesn't do the right thing. :P Commented Nov 25, 2012 at 21:45
  • @edit, use string::length. len = msg.lenght(); Commented Nov 25, 2012 at 21:55
  • error: âstruct std::stringâ has no member named âlenghtâ Commented Nov 25, 2012 at 21:58
  • If you want to use strlen: const char *cstr = msg.c_str(); Commented Nov 25, 2012 at 21:59

4 Answers 4

3

"stuff" + num + "more stuff" doesn't do what you'd expect. Even if you were to convert str to 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 + 0x10222250 or something like that, which would return you a pointer to some location that probably doesn't even exist yet...)

Try this:

string msg = string("GET /index.php?num=") + num + " HTTP/1.1\nhost: domain.com\n\n";
ssize_t bytes_sent = send(socketfd, msg.c_str(), msg.size(), 0);
Sign up to request clarification or add additional context in comments.

1 Comment

Hello, tried it. Got an error invalid conversion from âconst charâ to âcharâ
1

std::string is not implicitly converted to char*. You need to use c_str.

Comments

1

Ideally, you should work entirely with strings (not char*) in your application, right up to the point where an API function requires a char*, and at that point you call c_str on the string to get a const char* for the function you're calling.

Comments

1

You're using num where it is uninitialized on the third line. Maybe you want:

std::string num;
std::string msg = "GET /index.php?num=" + num + " HTTP/1.1\nhost: domain.com\n\n";

7 Comments

Yes. Is there a way to do that? Inserting the variable between? I mean an alternative? I guess thats whats making the error.
Why are you doing char + string + char in the first place?
I tried changing num into a char. but i get an error. error: invalid operands of types âconst char*â and âconst char [70]â to binary âoperator+â
@user1553142 Use std::string for all your string handling. It eliminates problems like these.
I tried it, and still got an error. error: cannot convert âstd::stringâ to âconst voidâ for argument â2â to âssize_t send(int, const void, size_t, int)â
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.