Sign in to follow this  
slayemin

help with string

Recommended Posts

I am having trouble with the STL string. I don't really know how to use it with anything else.
#include <string>
using namespace std;
...
string teststring = "testing123";

cout << teststring;
I know the above works because cout is pretty flexible with printing stuff, but when I have something that expects char *string it gives me some weird errors which I have not a clue as to how to fix:
#include <string>
using namespace std;

void PrintString(char *StringToPrint);
...
string teststring = "testing123";

PrintString(teststring);
...
Error: cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *' :P help! I don't know what to do.

Share this post


Link to post
Share on other sites
strings have a handy function, c_str() (for c-style string), which returns a const char *.


#include <string>

using namespace std;



void PrintString(char *StringToPrint);

...

string teststring = "testing123";



PrintString(teststring.c_str());

...

Share this post


Link to post
Share on other sites
Thanks! that worked very well.
One last question:

filename: enginecode.h

#include <fstream>
using namespace std;
...

ifstream IniFile; //<-error: 'ifstream' ambigous symbol

IniFile.open("Settings.ini");
...



I can't figure this out. I have other functions in other files within the project which use the same exact lines of code and they work. I don't understand what "'ifstream' ambigous symbol" means in this case.

Share this post


Link to post
Share on other sites
Quote:
Original post by slayemin
Thanks, oddly that worked. (???) I don't understand why I have to use std::ifstream on some variables but not on others

Basically that's what namespaces are for - resolving ambiguities.
If you put 'using namespace std;' everywhere, you prevent the parser from resolving name collisions (I cannot tell you why they appeared in your files, I don't have the full source and doesn't really matter). In that case you have to be explicit and add the namespace you meant manually.
The same might happen inside classes - suppose you have a class that features amember function called 'SetTimer()', which takes a pointer and two INT parameters. Now inside other class members you might need to manually set the scope of the function you want to call, because the windows API also contains a SetTimer() and the compiler will use the clas method (local scope) by default.
So in this scenario, if you want to call the actual Win32 'SetTimer()' you will need to put '::' in front of each SetTimer() call if you want the global function.

This might sound a little confusing but that's C++ [smile] - a fairly complex beast. You can read more about namespaces, name collisions and scopes here.
(there are better tutorials - just google for them if you want to know more).

Regards,
Pat.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this