![]() at the point where it was passed to the library that required it.īut, again, I can only see your simple example. I'd only convert to a char* at the point where it was actually needed - i.e. ![]() I'd make sure it was std::strings that were being passed back to main, and stored in main. I'd use std::string as much as I possibly could. That's a decision you'll have to make.Īctually, I wouldn't convert to a char* at all at that point. I mean, in the sample code you posted (and well done for making the effort to post a minimal example that demonstrates the problem - that really helps us!) I would allocate it dynamically at line 8, and copy the contents of the array pointed to by food.c_str() into the memory I've just allocated.īut for your actual project - which, I assume, is much bigger than this - that might not be the best fit. That's really a design question - you need to find the answer that best fits the design of your actual code. In which case, the best thing you can do is go back to your textbook and read those chapters. Which means you need to allocate that memory yourself.ĮDIT: If the things I'm saying don't make any sense to you, then I suspect it's because you've never really learned about pointers, or about memory allocation. You need to make sure name points to memory that's going to persist after the function exits. However, that points to memory managed by food when food is destroyed, that memory is freed up, so name now points to invalid memory. In your code, at line 8, you're setting the value of name to be the pointer returned by food.c_str(). In your code, not only have you not allocated memory, you haven't even given the char* pointer in the array any value it all. But just because you have a memory address, doesn't mean that you can just write data to that address, because for all you know that memory is being used for something important, if you're lucky. A pointer is just a number, and that number is a memory address. The point is that name is an array of pointers. But, even once you get this right, name isn't a string, it's an array of strings. ![]() You're then storing that char* in an array, for some reason. No, you're converting a string to a char*. \lib\gcc\mingw32\4.8.1\include\c++\mingw32\bits\c++config.Note that I'm converting from a string to a char** In addition to the changes explained in this post, I had to tweak the C++ config file: If you want to write a header which exposes a custom version of stoi (etc) just for MinGW GCC, then you can test against the #define _MINGW32_Įnabling string conversion functions in MinGW And you back everything up!)ĮDIT: the patch worked easily enough for 4.7.x, but appears to be broken for 4.8.1 :-(ĮDIT: with version 4.8.1 I had to make one change in addition to those explained in the post linked to below: see note below. something you should only attempt if you're sure fully undertand what you're doing. For info and caveats about the patch, see link below. easier to just code your own basic version if it's just for educational reasons. If you're compiler doesn't support stoi out of the box you will need to either switch to another mechanism or roll your own (see notes below.)įor Visual C++ you need version 2010 or higher for stoi.įor GCC I am not sure of the version stoi appeared in, but I do know that stoi is still broken in the MinGW version (version 4.8.1) I am assuming here that the file contains the ASCII values as strings, going by line 30 of the original code and the way the values display (i.e. (This is effectively doing the same work as stoi(), but when you read the value from the file.) ![]() Read the values into a int (or even char?) array, as Yanson suggested above. First convert the string to an int, and then display that int value as a char.ī. Error: cannot dynamic_cast 'messageIn' (of type 'std::string ') to type 'char' (target is not pointer or reference)Ī.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |