By the way, while C++ doesn't mind whether you use a C-style
(char)messageIn[f];
or a function style cast
char(messageIn[f]);
neither work here as you're trying to cast a std::string (a class) to a char, which would only work if std::string happened to provide the appropriate char cast operator, which is doesn't. None of static_cast<>, dynamic_cast<> and reinterpret_cast<> can handle this case, either.
MSVC wrote: |
---|
For C-style and function style casts, and static) cast (though message starts off with '<function-style-cast>' and 'static_cast' rather than 'type cast'.)
error C2440: 'type cast' : cannot convert from 'std::string' to 'char'
1> No user-defined-conversion operator available that can perform this conversion,
or the operator cannot be called |
For interpret_cast<> and const_cast<> the error code is the same, but the message is a bit different:
error C2440: 'static_cast' : cannot convert from 'std::string' to 'char'
1> Conversion requires a constructor or user-defined-conversion operator, which
can't be used by const_cast or reinterpret_cast |
And for completeness, attempting to use dynamic_cast<> results in a different error code:
error C2680: 'char' : invalid target type for dynamic_cast
1> target type must be a pointer or reference to a defined class |
|
GCC wrote: |
---|
For C-style and function style casts, and static_cast<> and reinterpret_cast<>
error: invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'char' |
For const_cast<>
error: invalid use of const_cast with type 'char', which is not a pointer, reference,
nor a pointer-to-data-member type |
And for dynamic_cast<>
error: cannot dynamic_cast 'messageIn[f]' (of type 'std::string {aka
class std::basic_string<char>}') to type 'char' (target is not pointer or reference) |
|
What you need to do here is either:
A. First convert the string to an int, and then display that int value as a char.
e.g. (using stoi)
cout << (char)stoi(messageIn[f]);
or
cout << char(stoi(messageIn[f]));
or even
cout << static_cast<char>(stoi(messageIn[f]));
Or...
B. Read the values into a int (or even char?) array, as Yanson suggested above. (This is effectively doing the same work as stoi(), but when you read the value from the file.)
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. your example 89 111 117 32 104 etc.......)
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.)
Andy
stoi
http://www.cplusplus.com/reference/string/stoi/
For Visual C++ you need version 2010 or higher for stoi.
For 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)
There is a patch, but it is prob. easier to just code your own basic version if it's just for educational reasons. For info and caveats about the patch, see link below. (Prob. something you should only attempt if you're sure fully undertand what you're doing. And you back everything up!)
EDIT: the patch worked easily enough for 4.7.x, but appears to be broken for 4.8.1 :-(
EDIT: 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.
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__
Enabling string conversion functions in MinGW
http://tehsausage.com/mingw-to-string
In addition to the changes explained in this post, I had to tweak the C++ config file:
<mingw-root>\lib\gcc\mingw32\4.8.1\include\c++\mingw32\bits\c++config.h
so line 1232 (in my case) read
#define _GLIBCXX_USE_C99 1
rather than
/* #undef _GLIBCXX_USE_C99 */
Macros __MINGW32__ AND __MINGW64__
http://mingw.5.n7.nabble.com/Macros-MINGW32-AND-MINGW64-td26319.html