Why don't I have to declare this namespace?

Hello All,

I have a question about namespaces. Here's an example program.

Header:

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
#ifndef __SALES__H
#define __SALES__H

namespace SALES
{
	const int QUARTERS = 4;

	struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};

	// copies the lesser of 4 or n items from the array ar
	// to the sales member of s and computes and stores the
	// average, maximum, and minimum values of the entered items;
	// remaining elements of sales, if any, set to 0
	void setSales(Sales & s, const double ar[], int n);

	// gathers sales for 4 quarters interactively, stores them
	// in the sales member of s and computes and stores the
	// average, maximum, and minimum values
	void setSales(Sales & s);

	// display all information in structure s
	void showSales(const Sales & s);

	double ave(Sales &s, int n);
	double min(Sales &s, int n);
	double max(Sales &s, int n);
}

#endif 


My Definitions:

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
#include <iostream>
#include "9.PE4.h"

namespace SALES
{
	// copies the lesser of 4 or n items from the array ar
	// to the sales member of s and computes and stores the
	// average, maximum, and minimum values of the entered items;
	// remaining elements of sales, if any, set to 0
	void setSales(Sales & s, const double ar[], int n)
	{
		int temp;
		if (n < 4)
			temp = n;
		else
			temp = 4;

		//Fills sales array
		int sales_count = 0;
		int i;		
		for (i = 0; i < temp; i++)
		{
			s.sales[i] = ar[i];
			sales_count++;
		}

		temp = 4 - sales_count;
		if (temp > 0)
		{
			
			for (temp = 4 - temp; temp < 4; temp++)
				s.sales[temp] = 0.0;
		}
		
		
		s.average = ave(s, sales_count);		
		s.max = max(s, sales_count);		
		s.min = min(s, sales_count);
		
	}

	// gathers sales for 4 quarters interactively, stores them
	// in the sales member of s and computes and stores the
	// average, maximum, and minimum values
	void setSales(Sales & s)
	{
		using std::cout;
		using std::cin;
		using std::endl;

		cout << "Winter: $";
		cin >> s.sales[0];
		cout << "Spring: $";
		cin >> s.sales[1];
		cout << "Summer: $";
		cin >> s.sales[2];
		cout << "Fall: $";
		cin >> s.sales[3];

		s.average = ave(s, QUARTERS);
		s.max = max(s, QUARTERS);
		s.min = min(s, QUARTERS);
	}

	// display all information in structure s
	void showSales(const Sales & s)
	{
		using std::cout;

		static int count = 1;

		cout << std::endl;
		cout << "~~~ Sales Structure # " << count << " ~~~\n";
		for (int i = 0; i < QUARTERS; i++)
			cout << "Quarter #" << i + 1 << ": " << s.sales[i] << std::endl;

		cout << "\nAverage: " << s.average << std::endl;
		cout << "Max: " << s.max << std::endl;
		cout << "Min: " << s.min << std::endl;
		count++;
	}

	//Maximum computation
	double max(Sales &s, int n)
	{
		double maximum = s.sales[0];
		for (int i = 1; i < n; i++)
		{
			if (s.sales[i] > maximum)
				maximum = s.sales[i];
		}
		return maximum;
	}

	//Minimum computation
	double min(Sales &s, int n)
	{
		double minimum = s.sales[0];
		for (int i = 1; i < n; i++)
		{
			if (s.sales[i] < minimum)
				minimum = s.sales[i];
		}

		return minimum;
	}

	//Average computation
	double ave(Sales &s, int n)
	{
		double dtemp = 0;
		for (int i = 0; i < n; i++)
			dtemp += s.sales[i];
		double average = dtemp / n;
		return average;
	}
}


Main Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "9.PE4.h"

int main()
{
	SALES::Sales s1[2];
	setSales(s1[1]);

	double Q1[6] = { 243.55, 22.11, 88.5, 235.7, 999.4, 88.7 };
	setSales(s1[2], Q1, 3);

	showSales(s1[1]);
	showSales(s1[2]);

	while (std::cin.get() == '\n');
}


Take a look at the first few lines of my main function. If I don't provide some kind of namespace declaration or scope resolution, my program won't recognize the "Sales" structure; but this situation is not the same with the functions in the same namespace.

Why don't I have to use a scope resolution operator on either of my overloaded "setSales" functions or my "showSales" function. I don't understand why this compiles correctly. I was expecting an "undefined" error.

I am using Visual Studio Community 2013.

Thank you for any clarification you are able to offer.
Last edited on
Topic archived. No new replies allowed.