Hello daniweb forum members!

Just a quick question which has hindered my programming greatly. I am creating an online role playing game:

vorii - http://www.youtube.com/watch?v=ePNxDlZ2H7Q

And I have come across a problem with the chat interface. The way I have programmed my game is that each player as a place in a vector for each category, E.G. vector<int> client_pos_y etc etc... and it has been working fine, but now I need to and char[], it has to be in this format as this is the format I send the strings of text and I want to manipulate them as char[]'s ...

I wondered if like vector<int> I could do vector<char[20]> but it doesn't seem to work, your ideas?

P.S I don't and won't use string, my GUI doesn't like it.

Thank you :-)
from helpfullprogrammer.

I wondered if like vector<int> I could do vector<char[20]> but it doesn't seem to work, your ideas?

No. Arrays don't meet the basic requirements of types suitable for storage in a vector (ex. assignment isn't allowed on arrays). You could use a dynamic C-style string and store char* in your vector, but that's probably not such a hot idea.

It seems to me a better overall solution is storing all of this stuff for each player in a custom class, then using a vector of objects of that class. I'm getting the impression that you have several vectors which could be merged into one.

P.S I don't and won't use string, my GUI doesn't like it.

This appears to be your underlying problem. Rather than hack a kludge into your code, you should probably work toward fixing the problem that encourages the kludge in the first place.

>>P.S I don't and won't use string, my GUI doesn't like it.

It doesn't like it most likely because you tried to pass std::string to a function that expects char*. To fix that all you have to do is use string's c_str() method. foo( mystring.c_str() );

Thank you everyone, yes stupid comment >> my GUI doesn't like it. It can accept it and that sounds very intresting, so I could do (vector<string> client_message). and for example when a function wants a char*, what would I put. if a vector of ints looks like:

client_message;
would you put client_message.c_str(i);

???

Thank you very much for taking your time to reply.
from helpfullprogrammer.

P.S if you want to see the project this is going to check out > http://www.youtube.com/watch?v=ePNxDlZ2H7Q !

to pass a string as a char * to a function if that string is in a vector you would do

Foo(client_message[i].c_str());

You guys are amazing, just one problem... I get the error:
<<<cannot convert parameter 3 from 'const char *' to 'char *'>>>

Is there a variation of this function that returns char* instead of const char*? Thank you for all of your replys!

from helpfullprogrammer.

Don't worry! found out the answer, &client_message[0]...

Thank you anyway!
from helpfullprogrammer.
:-)

As far as I know no. If the function you are passing the string to doesn't need to modify the string i would just change the function to except a const char * instead of a char *. If the function does need to modify the string or you cant change it you would have to copy the string into a c string and then pass that c string into the function.

You guys are amazing, just one problem... I get the error:
<<<cannot convert parameter 3 from 'const char *' to 'char *'>>>

Is there a variation of this function that returns char* instead of const char*? Thank you for all of your replys!

from helpfullprogrammer.

Have you not heard of type casts? foo((char*)client_message[i].c_str()); or foo( const_cast<char*>(client_message[i].c_str())); But you have to be careful about casting away the const, especially if function foo() wants to modify the contents of the string.

Edited 6 Years Ago by Ancient Dragon: n/a

Yes it will cause a huge problem -- and that's why I posted that disclaimer. If the function is going to modify the string then probably either use a vector of char* or copy the contents of the std::string into a char* and pass that copy to the function.

Your best bet I think, is to just write a linked list with the nodes containing a char array.

Have you not heard of type casts? foo((char*)client_message[i].c_str()); or foo( const_cast<char*>(client_message[i].c_str())); But you have to be careful about casting away the const, especially if function foo() wants to modify the contents of the string.

I strongly advise against doing that. If you need to cast away a const, then its a design problem. c_str() returns const char* for a reason.

This article has been dead for over six months. Start a new discussion instead.