I'm trying to create a program that would use a multitude of variables universally across my classes. My main class would house the variables in the beginning, but I seem to catch errors whenever I try to use the same strings or ints on a new class. Here's an example in case there is some confusion:
1 2 3 4 5 6 7
#include <iostream>
usingnamespace std;
string a;
int main()
{
}
I would have string a declared onto my main function, but it would not work the same way if I were to use the string in another class file. Is there a way in fixing it so that my main class's variables can be implemented into my other classes?
Your example doesn't explain anything, it has no classes.
what do you mean by this?
I would have string a declared onto my main function
To use the same variable across many classes that variable needs to be global, as your "string a" is above. Other classes need to declare it also so the compiler knows what to do.
So that you don't get multiple copies of the variable it needs to be declared "extern" in the other classes that use it.
This is considered bad practice and its use should be limited, giving a fuller explanation of your problem will help us all give you a more appropriate answer.
Whenever I declare the same string on my class, I would get an error saying that the string has multiple definitions. So if I wanted to use the main class as shown on my first post,
1 2 3 4 5 6 7
#include <iostream>
usingnamespace std;
string a;
int main()
{
//random code here
}
I want my said class to be able to recognize string a and utilize it as shown in the example .cpp class (it's in a separate file, sorry for not mentioning that):
1 2 3 4 5 6 7 8 9 10
#include "test.h"
#include <iostream>
usingnamespace std;
test::test()
{
a = "Hello";
cout << a << endl;
}
If I were to declare it in this class, I would get an error that says that there are multiple definitions of 'choice'.
void funky() {} //function
class MyClass {}; //class
If I were to declare it in this class, I would get an error that says that there are multiple definitions of 'choice'.
Is this 'choice' in your test.h header? Did you put up your include guards?
Ex:
1 2 3 4
#ifndef TEST_H //Check if already defined; if so, do not include code again
#define TEST_H //If not, go ahead and include code, but define TEST_H to prevent code duplication
//...
#endif //Make sure to tell the preprocessor that this is the end of #ifndef
Also, as Jaybob pointed out, to use variables in multiple files, you need to use the extern keyword.
Ex:
1 2 3 4 5 6 7
//test.h
extern std::string global_s; //indicates variable exists somewhere else
//...
//test.cpp
std::string global_s; //Where the variable is actually declared and stored
//...
Keep in mind, however, that global variables are usually unsafe as they can be modified unintentionally at any time, making them harder to manage and the program more bug-prone.
The 'choice' string is not in the test.h header. I'm quite confused on how to use the extern keyword so if it's possible for more explanation that would be great.
The reason I wanted to do this is because my program involves saving names over via strings. On the main class it'll have the names, but I want to use them for other classes as well.
Alright. I'm trying to make a simple DOS RPG program that consists of having the RPG elements of stats, items, and more put into variables. One such variable I would use is a string to name the player. It would be asked upon in the beginning and would be stored in a string known as 'playername'. I want to use that string across other classes so that other NPCs may refer to the name itself at some point since I have a separate class for dialogue.
Hope this was enough information to explain what I was trying to do. If not, I can give you my source code.
You could have public get functions which return the value of a particular private class variable. Only create get functions for the data you need to expose to the outside world. That is only for those variables which need to be used by another class. We had a huge discussion about this here :
I am mentioning this so that you are not tempted to abuse the use of the get functions. Quite often it is possible to not have any get / set functions, but sometimes it is easier to avoid massive coupling between all the classes.
Don't be tempted to have set functions which just straight assignment - that is really bad news!!
RPG are not as trivial as they may seem, they can involve quite a bit of OO design principles.
I am saying all this because I am guessing that if you want to use global variables, then that implies that you aren't aware of much about OO design either.