[VC++] Program does not take variables from other class

I just followed TheNewBoston's tutorials and made another class and header, but when I include header and write strings in cpp file, nothing happens. I am using Visual C++, Visual Studio 2013, I am on Windows 7, I tried moving header and cpp file in other folder but when I did ../, etc it could not even find header, anyways I need detailed and adapted answer, Thanks. and heres header file:

1
2
3
4
5
6
7
8
9
10
#pragma once
class TroopNames
{
public:
	
	TroopNames();
	~TroopNames();
};



and cpp file (not 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
#include "TroopNames.h"
#include <iostream>

using namespace std;

// Positions/Classes (Might change or be moved!)
string hGeneral = "Test"; // General
string generalName; // General (user)
string hKnight; // Horseman
string hManAtArms; // Horseman
string hSquire; // Footman
string hSpearman; // Footman
string hRecruit; // Footman/Spear
string hSwordsman; // Footman
string hChampionSwordsman; //Footman 
string hVeteranSwordsman; // Footman
string hCrossbowman; // Ranged
string hBowyer; // Ranged
string hSharpshooter; // Ranged/Crossbow
string hHunter; // Ranged

TroopNames::TroopNames()
{

}


TroopNames::~TroopNames()
{
}

(Sorry for silly code)
When I try to use one of these strings in main cpp, it does not work, I am not sure what I left out, or what I need to do but, yeah.

I can provide main.cpp too if needed.
Last edited on
When I try to use one of these strings in main cpp, it does not work
How do you use them? Did you declare them extern? Declare them again as empty strings?
I am little confused well, no I didn't, I did it so it won't take too much space in main cpp, but I rather keep those variables in maincpp than declaring them as extern, what would be advantage of that declaring them as extern? I mean, you can keep them in same class then?

Thanks for attention
Right now your variables are not in class. They are global variables shared by whole program.

You cannot access them unless you declare them extern in using compilation unit or you shadow them with another declaration (and use that declaration instead)

Just show how do you access them in main() and I tell you what is actually happening and why.
This is 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include "TroopNames.h"

using namespace std;

int main()
{
	default_random_engine randomEngine(time(0));
	uniform_real_distribution<float> attack(0.0f, 1.0f);

	
	// Human properties
	float humanAttack = 0.6f; //Chance
	float humanHealth = 250.0f;
	float humanDamage = 200.0f;
	float currentHumanHealth = humanHealth;
	int humansKilled = 0;

	// Skeleton properties
	float skeletonAttack = 0.2f;
	float skeletonHealth = 50.0f;
	float skeletonDamage = 40.0f;
	float currentSkeletonHealth = skeletonHealth;
	int skeletonsKilled = 0;

	float attackResult;

	// Armies
	int armyMen;
	int armySkeleton;

	char turn = 'H'; // H - Human

	cout << hGeneral << endl;
	cout << skeletonAttack << endl;
	cout << "" << "What your name would be?" << endl;
	cin >> generalName;
	cout << "Oh, welcome general " << generalName << endl;
	cout << "How many men do we have?" << endl;
	cin >> armyMen;
	cout << "I see..." << endl;
	cout << "((How many skeletons are attacking? (skeletons are weaker than humans)))" << endl;
	cin >> armySkeleton;

	if (armySkeleton <= 0)
	{
		cout << "Nice weather..." << endl;
	}
	else if (armySkeleton > 0 && armySkeleton <= 25)
	{
		cout << "Small group of skeletons is attacking!" << endl;
		cout << "We shall attack!" << endl;
	}
	else if (armySkeleton > 25 && armySkeleton < 100)
	{
		cout << "Big group of skeletons is attacking!" << endl;
		cout << "We shall attack!" << endl;
	}
	else if (armySkeleton >= 100)
	{
		cout << "Army of skeletons is attacking!" << endl;
		cout << "We shall attack!" << endl;
	}

	while ((armyMen > 0) && (armySkeleton > 0)) // Battle process
	{
		if (turn == 'H')
		{
			if (armyMen--)
			{
				cout << "Human recruit has been killed" << endl;
			}

			// Get attack result
			attackResult = attack(randomEngine);

			// Check if attack was succsessful
			if (attackResult <= humanAttack)
			{
				currentSkeletonHealth -= humanDamage;
				if (currentSkeletonHealth < 0)
				{
					armySkeleton--;
					skeletonsKilled++;
				}
				turn = 'S';
			}
		}
		else
		{
			if (armySkeleton--)
			{
				cout << "Skeleton recruit has been killed" << endl;
			}

			currentHumanHealth -= skeletonDamage;

			if (currentHumanHealth < 0)
			{
				armyMen--;
				humansKilled++;
			}
			turn = 'H';
		}

		cout << "Humans: " << armyMen << endl << "Skeletons: " << armySkeleton << endl;

		if (armyMen <= 0)
		{
			cout << "Victory!" << endl;
			cout << "Humans killed: " << humansKilled << endl << "Skeletons killed: " << skeletonsKilled << endl;
		}
		else if (armySkeleton <= 0)
		{
			cout << "Defeat" << endl;
			cout << "Humans killed: " << humansKilled << endl << "Skeletons killed: " << skeletonsKilled << endl;
		}
	}

	system("PAUSE");
}
This should not compile. As you are trying to use undeclared identifier.

I suggest to create an abstract Army class and then create instances of it with needed parameters.
I did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Army{
public:
	// Positions/Classes (Might change or be moved!)
	string hGeneral = "Test"; // General
	string generalName; // General (user)
	string hKnight; // Horseman
	string hManAtArms; // Horseman
	string hSquire; // Footman
	string hSpearman; // Footman
	string hRecruit; // Footman/Spear
	string hSwordsman; // Footman
	string hChampionSwordsman; //Footman 
	string hVeteranSwordsman; // Footman
	string hCrossbowman; // Ranged
	string hBowyer; // Ranged
	string hSharpshooter; // Ranged/Crossbow
	string hHunter; // Ranged

};


and made instance:

1
2
3
4
int main()
{
	Army armyObject;
...


but "Army" gives me error it says that identifier is undefined. There must be problem with including/finding header or TroopNames. any solutions?
Did you include header containing your army class?
Yes, sir.

1
2
3
4
5
#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include "TroopNames.h" 
Topic archived. No new replies allowed.