D
Doug Goulden
I have an application library that I have been using for some time with no
problems. Yesterday though I started to use part of the library and found
that the results were much different than I expected. My problem is in using
the std::string class, when I create a string by calling its constructor or
trying to set its value, the value of the string object is incorrect. The
difference between my
previous use and what I am doing no is that before I was creating strings
that might be only 8 - 10 characters long
now they can be as long as 20 - 30 characters. Apparently my problem lies in
the length of the string and resizing it.
If in my code I use the constructor -
string string2("This is a test");
the value is what I would expect, however if I call the constructor with a
longer string of say 20 or 30 characters, the string value is not the same
as the value in the constructor.
For example -
string string2("This is a test of a much longer string that will fail");
If I create the string and try to either call the resize function and then
assign(or append, or copy) the value into the string, the result is still
the same. I have read through the documentation for my compiler (MS Visual
Studio .Net ... sorry) and reviewed several websites and books. I don't see
what I am missing. Could somebody post some pseudo code that would
demonstrate how to create a string, resize it to a set length and then set
its value? My thinking is it should be something like this.... but it
doesn't work.
// Assuming testString is pointing to a string of char that is = to
length.
void SetStringValue(char* testString, int length) {
string test;
test.resize(length);
test = testString;
cout << string;
}
problems. Yesterday though I started to use part of the library and found
that the results were much different than I expected. My problem is in using
the std::string class, when I create a string by calling its constructor or
trying to set its value, the value of the string object is incorrect. The
difference between my
previous use and what I am doing no is that before I was creating strings
that might be only 8 - 10 characters long
now they can be as long as 20 - 30 characters. Apparently my problem lies in
the length of the string and resizing it.
If in my code I use the constructor -
string string2("This is a test");
the value is what I would expect, however if I call the constructor with a
longer string of say 20 or 30 characters, the string value is not the same
as the value in the constructor.
For example -
string string2("This is a test of a much longer string that will fail");
If I create the string and try to either call the resize function and then
assign(or append, or copy) the value into the string, the result is still
the same. I have read through the documentation for my compiler (MS Visual
Studio .Net ... sorry) and reviewed several websites and books. I don't see
what I am missing. Could somebody post some pseudo code that would
demonstrate how to create a string, resize it to a set length and then set
its value? My thinking is it should be something like this.... but it
doesn't work.
// Assuming testString is pointing to a string of char that is = to
length.
void SetStringValue(char* testString, int length) {
string test;
test.resize(length);
test = testString;
cout << string;
}