Multiple Definition of 'string variable' Compiling error

I've been writing code just in main.cpp, but I wish to clean everything up and separate files into header as well as a definitions source file.

I understand the concept(to a small extent)of the #include insofar as it basically copy/pastes the stuff(to put it simply) - if this is incorrect, do correct my ignorance.

I keep getting this: "multiple definition of 'user_input' ".

So here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
#include "declarations.h"


int main()

{
user_input = "";
std::cout<<"Welcome to your new life. If you are not familiar with text-based games, that's okay -- as long as you can read. If you can read, everything else will fall into place. \n"<<std::endl;
std::cout<<"Our tutorial will become larger as the game develops, as will the gaming environment, so bear with us as we continue providing updates and items to the game. \n"<<std::endl;


while(user_input != "start")
{
    std::cout<<"Tye the word \"start\" to begin: ";
    std::getline(std::cin, user_input);
    if(user_input == "start")
    {
    Character_Setup();
    //user_input = "";

    }
    else
        std::cout<<"You have entered an invalid response. Please type \"start\" to begin.\n";


}


}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//declarations.h header file
#ifndef _DECLARATIONS_H_INCLUDED
#define _DECLARATIONS_H_INCLUDED
//#include "definitions.cpp"


std::string user_input;
void Character_Setup();
struct Player_Character
{
    std::string name;
    int age;
    std::string race;
    std::string class_type;
    std::string profession;
    struct attributes
    {
        int Stamina_att;
        int Strength_att;
    };
};





#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//definitions.cpp
#include <iostream>
#include "declarations.h"
void Character_Setup()
{
    std::string user_input_char_setup;
    int completion;
    while(completion=0)
    {
        std::cout<<"What would you like your character name to be?\n";
        //std::getline(std::cin, user_input_char_setup);

    }

}


My error comes on the beginning brace of int main on my main.cpp -- line 8.

Now, I've changed the '#includes' around and have received various errors such as 'not defined' or 'redefinition of 'void character_setup'.

I've been trying to understand this for awhile now; I can't so I'm asking the community to maybe explain the functionality of this in a way that I'll be able to fix it because I understand it rather than fix it because I know I'm supposed to put 'this' here or 'that' there..if that makes any sense.

Thank you in advance.

To add, I added variables like 'user_input_char_setup' just to test some things to fix the problem. So they will seem out of place because they are.
Last edited on
To add, I can fix the error by making std::string user_input a global variable above main. When I do that, I get 0 warnings or errors.

BUT, it won't go to Character_Setup(); when I type in start after it asks me to type start. . it just ends the program right there. ???
Welp, I fixed this one too. Wow. I worked on this for like 3 hours, only to post on here and fix it in less than 5 minutes. *sigh*

I did that earlier as well. :(

I would still like to understand why I can't declare a global variable in my header file without getting that stinkin' error message. :(
Topic archived. No new replies allowed.