Carry a string?

closed account (j6XEy60M)
Hi, I'm having a little problem with a string variable in my program.
I usually like to figure out problems myself, but this one has stopped my work for two days now and I'm getting ticked. Yes, you're going to see that I'm a noob, so please be patient with me: I have no friends who program to ask.
Anyway, the problem: I'm creating a game where I need to carry the character's name (string szPlayersName;) from function to function, .cpp to .cpp file, you get the idea. I am not very efficient in the first place so I am looking for just about any solution that will work. If there is a way to place it in a header file without having to #include <string> at the top (since it's impossible in a header) that'd be great, but otherwise just about anything goes.

Thanks from the noob.
Ah. Multi-file stuff. <3.

You'll need a header file in which you will declare your function and any stuff that will be either used or implemented (inclusively) by either file. This header file will be included by both your main C++ file and other C++ files (any that will use your functions or implement them). In your other C++ files, you will merely implement the function or call ones already implemented in other file. Make sure you don't implement the same function in multiple C++ files. Does that make sense?

Oh, and #including <string> in a header is possible, AFAIK. What are you talking about?

-Albatross
Last edited on
closed account (j6XEy60M)
It is? I've tried it twice now and it's not working. Maybe I'm placing it wrong. Mind taking a look at my header?

#ifndef SHARED FUNCTIONS_H
#define SHARED FUNCTIONS_H

#include <string>

string szPlayersName; //it's also saying I'm missing a ";" before "szPlayersName" beats me.

int main();
int titlescreen();
int newgame();
int mainmenu();
int opening();

#endif

Thanks for trying to help out so far.
Yeah... I've had some experiences with namespaces when I was a novice, suffice it to say (Yoda grammar, hehe).

It's std::string (not just string unless you're using something, and that's bad form in a header file).

And... by the way... you can't do what you're doing with the variable. You'll get a linker error for the redeclaration of szPlayersName across multiple files. Try:
extern std::string szPlayersName;
...and redeclare std::string szPlayersName; in ONE of your C++ files. extern basically says it's from another file or redeclared later in the file.

-Albatross
Last edited on
closed account (j6XEy60M)
Output:
string is not a member of std

I'm not going to bore you with the rest of the output, this is the only new one that I can't solve.
I know I probably sound pretty clueless, I am when it comes to this and probably will be until college when I'm surrounded by professors and programming will be my life (yay).

If I can't solve this, it's no big deal. I haven't got that far into making this program anyway.
*mutters curses designed to blow up the fabric of reality*
What file is this error from? Are you sure you're #including your header file in that file if it's a C++ file?

-Albatross
Last edited on
closed account (j6XEy60M)
Holy crap, it works now!!!! (has quick celebratory moment since no one's looking)

Thanks, it works now. I was including my header file, but was forgetting something else that I had accidentally deleted during this entire process. Thanks for sticking with me. You're beast.
Topic archived. No new replies allowed.