In your second listing (function definitions) the functions you define do not belong to any namespace. using namespace SALES; merely informs the compiler to decorate referred names. You should put them in a namespace the same way you did in the header file, that is inside namespace SALES {...}
OK, you're absolutely right, but after adding
namespace SALES { around line 8 of the functions file (and a closing curly at the end), it's still giving me the same compile error -- undefined reference to foo.
I am able to get it to compile, minus a total variable that appears out of nowhere and some cin's that had the << operator instead of the >> (I guess I caught your code before you changed it). It was balking about the signature of the setSales function not matching but that resolved. What is the error you are getting now? It could have to do with your namespaces and needing a using statement in main but I didn't test that.
The "total" was supposed to be "sum," oops. I changed it for no good reason midway -- bad Grib! I haven't even managed to get the functions to load into the compiler; it's getting hung up in main() and not even trying to compile the functions. Curremtly, that file looks like:
// Exercise 9.4 function definitions
#include <iostream>
#include "exercise9.4_header.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.
The remaining elements of sales, if any, get set to 0. */
void setSales(Sales & s, const double ar[], int n)
{
using namespace std;
double sum;
if (n = 0)
{
for (int i = 0; i < QUARTERS; i++)
s.sales[i] = 0;
s.min = 0;
s.max = 0;
s.average = 0;
return;
}
s.min = ar[0];
s.max = ar[0];
for (int i = 0; i < n; i++)
{
s.sales[i] = ar[i];
sum += ar[i];
if (ar[i] < s.min)
s.min = ar[i];
else if (ar[i] > s.max)
s.max = ar[i];
}
for (int i = n; i < QUARTERS; i++)
{
s.sales[i] = 0;
}
s.average = sum / n;
return;
}
/* 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 namespace std;
double ar[QUARTERS];
int n;
cout << "\nHow many quarters will you input data in this year? _\b";
while ((!(cin >> n)) && n >= 0 && n <= QUARTERS)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "\nInput a number from 0 to " << QUARTERS << ", please: _\b";
}
for (int i = 0; i < n; i++)
{
cout << "\nInput sales for quarter #" << i << ": ";
while (!(cin >> ar[i]))
{
cin.clear();
cin.ignore(1000, '\n');
cout << "\nInput a number, please. ";
}
}
setSales(s, ar, n);
return;
}
// display all information in structure s
void showSales(const Sales & s)
{
using std::cout;
using std::endl;
for (int i = 0; i < QUARTERS; i++)
cout << "Quarter #" << i + 1 << ": $" << s.sales[i] << endl;
cout << "Average quarterly sales: $" << s.average << endl;
cout << "Worst quarter's sales: $" << s.min << endl;
cout << "Best quarter's sales: $" << s.max << endl;
return;
}
}
...and the errors I'm getting are, specifically:
obj\Debug\exercise9.4_main.o||In function `main':|
|16|undefined reference to `SALES::setSales(SALES::Sales&)'|
|17|undefined reference to `SALES::showSales(SALES::Sales const&)'|
|19|undefined reference to `SALES::setSales(SALES::Sales&, double const*, int)'|
|20|undefined reference to `SALES::showSales(SALES::Sales const&)'|
|22|undefined reference to `SALES::setSales(SALES::Sales&, double const*, int)'|
|23|undefined reference to `SALES::showSales(SALES::Sales const&)'|
||=== Build finished: 6 errors, 0 warnings ===|