JrMasterModelBuilder Posted October 19, 2011 Share Posted October 19, 2011 After all the complex coding I've gone though in this project, it seems silly to stumble over something that seems so simple, but here's the run down and hopefully someone will be able to help! As the exact code I'm working with is very complex and I don't have the license to distribute all of it, I have cut it down to what I'm almost certain is the problem. The library I have to use for this project requires I convert the string to "const uint8_t*" like you can see in my code below. Doing this with a string I enter directly into the code will still output as a string by cout (and also into the library code as expected), however trying to convert a dynamically created string (even with .c_str() called on it) results in nothing (or if I try really hard, gibberish). #include <iostream> #include <conio.h> #include <string> //For the variable type I want to convert to. #include <stdint.h> using namespace std; //Create dynamic string. string getstring() { string s = "dynamic"; s += " string"; return s; } int main() { cout<<"OUTPUT1:\n\n"; //This will work with the simple string. const uint8_t* msg = (const uint8_t*)"plain string"; cout<<msg; cout<<"\n\n\n"; cout<<"OUTPUT2:\n\n"; //This won't work with the dynamic string. const uint8_t* msg2 = (const uint8_t*)getstring().c_str(); cout<<msg2; cout<<"\n\n\n"; //Wait for input. cout<<"END OUTPUT\nPress any key to continue . . ."; _getch(); return 0; } This code I am using in a concole application I made in Microsoft Visual C++ Express 2010. If anyone can help, that would be amazing! I've tried everything I know and could find on Google (which was almost nothing). Link to comment Share on other sites More sharing options...
JrMasterModelBuilder Posted October 19, 2011 Author Share Posted October 19, 2011 Never mind. Substituting const uint8_t* msg2 = (const uint8_t*)getstring().c_str(); cout<<msg2; for string str(getstring()); const uint8_t* msg2 = (const uint8_t*)str.c_str(); cout<<msg2; works perfectly. IDK what the difference is, but I'm happy. :) Link to comment Share on other sites More sharing options...
Phoenyx Posted October 19, 2011 Share Posted October 19, 2011 Never mind. Substituting const uint8_t* msg2 = (const uint8_t*)getstring().c_str(); cout<<msg2; for string str(getstring()); const uint8_t* msg2 = (const uint8_t*)str.c_str(); cout<<msg2; works perfectly. IDK what the difference is, but I'm happy. The likely difference is that MVC++ does not use the namespace standard (even though you defined that the program does, the line "using namespace std;", from my experience, does nothing in MVC++). I've fooled around with MVS a lot but I could never figure out why programs I write in Dev C++ won't compile in MVC++. Link to comment Share on other sites More sharing options...
JrMasterModelBuilder Posted October 20, 2011 Author Share Posted October 20, 2011 I'm 99.9% sure "using namespace std;" in MSVC does have an effect. The following hello world program won't work without it. #include <iostream> #include "conio.h" using namespace std; int main() { cout << "Hello, world!\n"; _getch(); return 0; } Though I total see what your saying about differences in compilers. What I'm working on was once being compiled with MinGW, but now I'm porting that functionality to a DLL with other functionality only available in for MSVC, and I had to rewrite a few things it didn't like but weren't a problem in MinGW. Link to comment Share on other sites More sharing options...
Recommended Posts