Public Group

# help with string

This topic is 5068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Thanks! that worked very well.
One last question:

filename: enginecode.h
#include <fstream>using namespace std;...ifstream IniFile;  //<-error: 'ifstream' ambigous symbolIniFile.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 on other sites
Have you accidently named some variable 'ifstream'?

##### Share on other sites
no..
I even copy/pasted my code to another file that has working ifstream variables & declarations but now one of the working declarations is now showing up as "ifstream" ambiguous symbol. :(((

##### Share on other sites
Try replacing ifstream with std::ifstream where you get the error and see if that works.

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

##### Share on other sites
Quote:
 Original post by slayeminThanks, 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.

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009405
• ### Who's Online (See full list)

There are no registered users currently online

×