Multiple Defintions

Pages: 12
Everything compiles fine but it wont run because of multiple defintions here is my codes.

main,cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include "inn.h"
#include "globals.h"
#include "Store.h"
using namespace std;

int main()
{

    int PlayerChoice, e, E;
    char UserName[12];

    cout << "Welcome to Cinz\n";
    system("pause");
    cout << "What would you like to do?\n";
    cout << "1) Play\n";
    cout << "E) Exit\n";
    cin >> PlayerChoice;

    if(PlayerChoice == 1)
    {
    cout << "Many years ago when I was child my dad left he told me keep your mother safe I will be back tomorrow. I sat there at the window for days just waiting for him to return. Everytime I fell asleep all I could see was his face fading away from my eyes. It had been years before I let him go. Now everytime I here his name Rale. My body builds up constant rage. Now that he is gone I have set out to avenge her.\n\n";
    system("pause");
    cout << "What is your name: ";
    cin >> UserName;
    cout << "Hello " << UserName << " What would you like to do?\n\n";

    cout << "1) Go to inn\n";
    cout << "2) Go to Store\n";
    cout << "3) Go exploring\n";
    cout << "4) Character page\n";
    cout << "5) Inventory page\n";
    cout << "E) Exit Program\n";
    cin >> PlayerChoice;
}
    if(PlayerChoice == 1)
    {
     Inn go;
}
     else if(PlayerChoice == 2){
     Store go;
          }

 else if(PlayerChoice == E || e)
 {
      return 0;

}
return 0;
}


inn.cpp
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
#include "inn.h"
#include <iostream>
using namespace std;

int PlayerChoice, e, E;
char UserName[12];

Inn::Inn()
{

do{
          cout << "Welcome to the Inn How may I be of service today?\n";
          system("pause");
          cout << "1) Rest\n";
          cout << "E) Exit the inn\n";
          cin >> PlayerChoice;

                          cout << UserName << "you are now well rested\n\n";
                          }
          while(PlayerChoice == 1);
         if(PlayerChoice == E || e){

                            }

}


inn.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef INN_H
#define INN_H

class Inn
{
	public:

		Inn();
};

#endif  



store.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef STORE_H
#define STORE_H

class Store
{
	public:

		Store();
};

#endif 


store.cpp
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
32
33
34
35
36
#include "store.h"
#include "Weapons.h"
#include <iostream>
using namespace std;

int PlayerChoice, e, E;
char UserName[12];

Store::Store()
{
              do{
              cout << "\nWelcome to the store we have many artillery and defense.\n";
              system("pause");
              cout << "1) Weapons\n";
              cout << "2) Armor\n";
              cout << "3) Shields\n";
              cout << "E) Exit Store\n";
              cin >> PlayerChoice;
              }
              while(PlayerChoice == 1 || 2 || 3 || E || e);
              
              if(PlayerChoice == 1){
                              Weapons go;
                              }
              else if(PlayerChoice == 2){
                   
                   }
              else if(PlayerChoice == 3){
                   
                   }
              else if(PlayerChoice == E || e){
                                
                                }


}


weapons.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef WEAPONS_H
#define WEAPONS_H

class Weapons
{
	public:
		
		Weapons();
};

#endif 


weapons.cpp
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
#include "weapons.h"
#include "store.h"
#include <iostream>
using namespace std;

int PlayerChoice, e, E;


Weapons::Weapons()
{
do{
   cout << "Choose a weapon to purchase. Input (E) to go back to store\n";
   system("pause");
   cout << "1) Paper Sword - 10g\n";
   cout << "2) Rubber Sword - 25g]n";
   cout << "3) Ice Sword - 35g\n";
   cout << "4) Wood Sword - 50g\n";
   cout << "5) Iron Sword - 100g\n";
   cout << "6) Gold sword - 200g\n";
   cout << "7) Crystal Sword - 500g\n";
   cout << "8) Platinum Sword - 1000g\n";
   cout << "9) Dragon Blade - 5000g\n";
   cin >> PlayerChoice;
}
while(PlayerChoice == 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9);

if(PlayerChoice == E || e){
                Store go;
                }
}
You are declaring the same things in the same scope repeatedly.

int PlayerChoice, e, E;
char UserName[12];



Declare them once, in one place, and either pass them around as function parameters, or if you insist on global variables, use extern
How would I go about passing them as a function? the tutorial on this site didn't explain well very well how to pass an int as a fucntion?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

int main ()
{
  int z;
  int x = 4;
  int y = 6;
  z = addition (x,y);
  cout << "The result is " << z;
  return 0;
}


Like that. It passes two int values into the function.
So I would right it like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef GLOBALS_H
#define GLOBALS_H

class Globals
{
	public:
		Globals(
int PlayerChoice, e, E;
Char UserName[12]
);

};

#endif  
How would i go about this using as extern. where i could just include the .h file to each .cpp file so i don't have to re-declare I used extern before but, it didn't work. Was I suppose to set extern in the .cpp and not in the .h but, include the .h
in place of the ifndef, define, endif.. you could use #pragma once
So far in programming I have yet to reach #pragma I have at least some experience with extern could we try this.
I have made all my ints and chars into extern and now i am getting a linker error any idea?
You also have to define the variables in a source file
closed account (3hM2Nwbp)
in place of the ifndef, define, endif.. you could use #pragma once


But be sure that your compiler supports it!

1
2
3
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma once
#endif 
Last edited on
Seriously? If you are going to use preprocessor directives to check if #pragma once is supported, it's much better to just use normal include guards that is guaranteed to work everywhere.
closed account (3hM2Nwbp)
@Peter, I couldn't agree more...however it is not good practice to use non-standard elements without protecting translation units from it. That directive prevents compatible compilers from opening and preprocessing files more than once. Typically, with include guards, files are opened and preprocessed, even if they've already been included. It's a cheap (usually un-noticed) trick to reduce compilation times, thus, even if include guards are used, it can still help to reduce compilation times on compatible compilers.

I personally do not use #pragma once .

* It also makes having a include guard naming collision impossible.

Final Edit, I promise...
** The boost library uses #pragma once in addition to standard include guards.
Last edited on
@peter you say I must define variables in a source file. I have a class called globals.cpp and .h So I would declare them in the .h and, inclue that with all my classes that those.
1
2
3
// globals.h
extern int SOME_GLOBAL;
extern char SOME_OTHER_GLOBAL;

1
2
3
// globals.cpp
int SOME_GLOBAL = 3;
char SOME_OTHER_GLOBAL;
Ok did what you guys said. now let me be blunt we are going in a big circle lets thing before we talk. but, thank you also peter because this will save me time later and i...Wait I will post back in a moment i think i may have idea why not working. multiple defition of 'PlayerChoice'
multiple definition of `e'
multiple definition of `E'
multiple definition of `UserName'
Last edited on
Ok i checked my idea. It comes out to same error but, I just saved like 5-8KB data.
Last edited on
any idea onwhy getting multiple defintion error now?

error:

multiple definition of `e'
multiple definition of `E'
multiple definition of `UserName'

globals.cpp
1
2
3
4
5
6
7
#include "globals.h"

globals::globals()
{
extern int PlayerChoice, e, E;
extern char UserName[12];
}


inn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "inn.h"
#include "globals.h"
#include <iostream>
using namespace std;

Inn::Inn()
{

do{
          cout << "Welcome to the Inn How may I be of service today?\n";
          system("pause");
          cout << "1) Rest\n";
          cout << "E) Exit the inn\n";
          cin >> PlayerChoice;

                          cout << UserName << "you are now well rested\n\n";
                          }
          while(PlayerChoice == 1);
         if(PlayerChoice == E || e){

                            }

}


main.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include "inn.h"
#include "globals.h"
#include "Store.h"
using namespace std;

int main()
{



    cout << "Welcome to Cinz\n";
    system("pause");
    cout << "What would you like to do?\n";
    cout << "1) Play\n";
    cout << "E) Exit\n";
    cin >> PlayerChoice;

    if(PlayerChoice == 1)
    {
    cout << "Many years ago when I was child my dad left he told me keep your mother safe I will be back tomorrow. I sat there at the window for days just waiting for him to return. Everytime I fell asleep all I could see was his face fading away from my eyes. It had been years before I let him go. Now everytime I here his name Rale. My body builds up constant rage. Now that he is gone I have set out to avenge her.\n\n";
    system("pause");
    cout << "What is your name: ";
    cin >> UserName;
    cout << "Hello " << UserName << " What would you like to do?\n\n";

    cout << "1) Go to inn\n";
    cout << "2) Go to Store\n";
    cout << "3) Go exploring\n";
    cout << "4) Character page\n";
    cout << "5) Inventory page\n";
    cout << "E) Exit Program\n";
    cin >> PlayerChoice;
}
    if(PlayerChoice == 1)
    {
     Inn go;
}
     else if(PlayerChoice == 2){
     Store go;
          }

 else if(PlayerChoice == E || e)
 {
      return 0;

}
return 0;
}


weapons.cpp
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
#include "weapons.h"
#include "store.h"
#include "globals.h"
#include <iostream>
using namespace std;

Weapons::Weapons()
{
do{
   cout << "Choose a weapon to purchase. Input (E) to go back to store\n";
   system("pause");
   cout << "1) Paper Sword - 10g\n";
   cout << "2) Rubber Sword - 25g]n";
   cout << "3) Ice Sword - 35g\n";
   cout << "4) Wood Sword - 50g\n";
   cout << "5) Iron Sword - 100g\n";
   cout << "6) Gold sword - 200g\n";
   cout << "7) Crystal Sword - 500g\n";
   cout << "8) Platinum Sword - 1000g\n";
   cout << "9) Dragon Blade - 5000g\n";
   cin >> PlayerChoice;
}
while(PlayerChoice == 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9);

if(PlayerChoice == E || e){
                Store go;
                }
}


store.cpp
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
32
33
34
#include "store.h"
#include "globals.h"
#include "Weapons.h"
#include <iostream>
using namespace std;

Store::Store()
{
              do{
              cout << "\nWelcome to the store we have many artillery and defense.\n";
              system("pause");
              cout << "1) Weapons\n";
              cout << "2) Armor\n";
              cout << "3) Shields\n";
              cout << "E) Exit Store\n";
              cin >> PlayerChoice;
              }
              while(PlayerChoice == 1 || 2 || 3 || E || e);
              
              if(PlayerChoice == 1){
                              Weapons go;
                              }
              else if(PlayerChoice == 2){
                   
                   }
              else if(PlayerChoice == 3){
                   
                   }
              else if(PlayerChoice == E || e){
                                
                                }


}


globals.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef GLOBALS_H
#define GLOBALS_H

class globals
{
	public:

		globals();

};

int PlayerChoice, e, E;
char UserName[12];

#endif 


inn.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef INN_H
#define INN_H

class Inn
{
	public:

		Inn();
};

#endif  


store.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef STORE_H
#define STORE_H

class Store
{
	public:

		Store();
};

#endif 


weapons.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef WEAPONS_H
#define WEAPONS_H

class Weapons
{
	public:
		
		Weapons();
};

#endif 
If you are going to have the global variables you don't need to have the globals class. The extern declarations should go in the header and the declarations should be in the source file, and everything should be outside class/function defintions.
As an aside,

while(PlayerChoice == 1 || 2 || 3 || E || e)
will ALWAYS come out as true.

Did you mean this?
1
2
3
4
5
while(PlayerChoice == 1 ||
      PlayerChoice == 2 ||
      PlayerChoice == 3 ||
      PlayerChoice == E ||
      PlayerChoice == e)
Last edited on
Pages: 12