simple but not working

This is a discussion on simple but not working within the C++ Programming forums, part of the General Programming Boards category; Why would the following give an error as shown?
Code:
std::string messageOut = "header 1: " + "test";
error: invalid ...

And for the last hour I've been discovering that whenever and however I invoke DecToBin(num)...
e.g.

Code:

test1 = DecToBin(num);
messageOut = "header1: " + test1;

...that the executable returns "segmentation fault". I step through the code and find that a normally solid function (elsewhere and upstream in the method) returns negative.

What I'm wondering is that because I've defined DecToBin(int num) as an outside function to the class method that calls it, is it possible that some of its internal char* methods are conflicting with the class methods memory allocations?

>> I don't think you should return local objects.
It's fine if it is a string object, which is what that function returns.

>> Because string literals are char *'s when you get down to it.
I'm not referring to string literals. I'm referring to actual usage and manipulation of the string itself. It sounds as if Dave++ is using char*'s as variables, when all that does is lead to greater potential for errors like segmentation faults. The C++ string class should be used instead.

The only char*'s you might want are const char*'s to point at the string literals, but even those are probably not necessary.

More testing...
In gdb I can comment out the call to the function, with a simple default value in its place, and then reset the DAQ hardware and everything works fine, but then as soon as I uncomment the line...everything fails. AND then when I comment the line out again errors persist until I reset the hardware.
Interestingly, the function itself works alright (set var=newval, and step though an if) but its inclusion alone seems to cause the code to fault ahead of itself and then behind as well.

So, if I get you right, I need to create a global pointer for the string?

>> So, if I get you right, I need to create a global pointer for the string?
No. If you are returning a string, you don't have to worry about that (Macgyver was mistaken). But it really depends on the code, its hard to tell just by your description.

The problem is likely that you didn't allocate space for the char* when you moved it inside the function. You could declare str as char str[100] if you are using mike_g's version, but I would use a bitset as I mentioned in that thread:

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell