Multiple Defintions

Pages: 12
@peter87

Could you give an example do u mean like

1
2
3
#include "extern" 
or
#include <extern> 


also @moschops

No my way saves space within program. My way works lol.
Last edited on
I gave you an example earlier but I can post it again.

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;


In files that you are going to use these globals you just include globals.h.
My way works lol.


You can save even more space; your code creates an identical effect to

while (true);

so you can just do that instead. It's not what you intended your code to do, but if saving space is more important than correct operation, go with it.
Last edited on
@peter I did what you said earlier lol. look

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 


also, here is a .cpp file I used it in

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){
                                
                                }


}
But you do it the wrong way. extern should be in the header.
Im not sure you understand the coding I wrote it in globals.h and used it in my .cpp files.

are you saying something like this.

[code]
#include "extern"
When you #include something, the preprocessor copies it exactly into where you put it. So everywhere that you #include "globals.h", you are pasting this:

1
2
3
4
5
6
7
8
9
10
class globals
{
	public:

		globals();

};

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


Thus, every cpp file you #include "globals.h" in has

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


pasted into it.

Where's the extern? Nowhere. So you've got this:

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


in every cpp file at the global level. Which means you've tried to define the same objects over and over again, which is exactly what you were doing at the start.
Last edited on
im not using - sorry starting to be get irritated with this. We just keep going in a big circle.
1
2
int PlayerChoice, e, E;
char UserName;


im using
1
2
extern int PlayerChoice, e, E;
extern char UserName;



I did it for you. You will note that there is one of these:
1
2
int PlayerChoice, e, E;
char UserName;

and all the rest are
1
2
extern int PlayerChoice, e, E;
extern char UserName;


Now it compiles, and you can get on with fixing the logic errors.


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

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


int main()
{

   

    cout << "Welcome to Cinz\n";
    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";
    
    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
#include "inn.h"
#include <iostream>
using namespace std;

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

Inn::Inn()
{

do{
          cout << "Welcome to the Inn How may I be of service today?\n";
                    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.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;

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

Store::Store()
{
              do{
              cout << "\nWelcome to the store we have many artillery and defense.\n";
              
              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){
                                
                                }


}



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.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;

extern int PlayerChoice, e, E;


Weapons::Weapons()
{
do{
   cout << "Choose a weapon to purchase. Input (E) to go back to store\n";
  
   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;
                }
}



weapons.h

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

class Weapons
{
	public:
		
		Weapons();
};

#endif  




yeah I know the way you did it works. but, I was trying to declare it once in 1 file and have it be able to use in all the rest. thats why I made globals.h
globals.h

1
2
extern int PlayerChoice, e, E;
extern char UserName[12];


Put

#include "globals.h"

everywhere in my code that currently has

1
2
extern int PlayerChoice, e, E;
extern char UserName[12];


Where my code currently has
1
2
int PlayerChoice, e, E;
    char UserName[12];

leave it there and do not put
#include "globals.h"
Last edited on
Topic archived. No new replies allowed.
Pages: 12