Had a moment to sit down and plow through a chapter, and as usual, wound up stuck. My compiler is giving me a string of undefined reference to SALES::[all my function names] in my main function. It's entirely possible that there are problems within the function definitions, but right now I can't even get the compiler to take a look at them, since my design is somehow failing to call them from main().

// exercise9.4 header file

#ifndef EXERCISE9_4_HEADER_H_INCLUDED
#define EXERCISE9_4_HEADER_H_INCLUDED

namespace SALES
{
    const int QUARTERS = 4;
    struct Sales
    {
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    };

    void setSales(Sales & s, const double ar[], int n);  //non-interactive

    void setSales(Sales & s);  //interactive

    void showSales(const Sales & s);  //display
}
#endif // EXERCISE9_4_HEADER_H_INCLUDED
//  Exercise 9.4 function definitions
#include <iostream>
#include "exercise9.4_header.h"

using namespace std;
using 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)
{
   //stuff, can post if necessary
}

/*  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)
{
   // more stuff
}

//  display all information in structure s
void showSales(const Sales & s)
{
//display-ish stuff
}
//  Exercise 9.4 main function
#include <iostream>
#include "exercise9.4_header.h"

using namespace std;

int main()
{

    using namespace SALES;
    Sales test;
    for (int i = 0; i < QUARTERS; i++)
        test.sales[i] = 0;
    double testArrayA[QUARTERS] = {0.0, 33.3, 66.7, 100.0};
    double testArrayB[QUARTERS] = {25, 75, 0, 0};
    setSales(test);
    showSales(test);
    cout << "Test Array A:\n";
    setSales(test, testArrayA, 4);
    showSales(test);
    cout << "Test Array B:\n";
    setSales(test, testArrayB, 2);
    showSales(test);

    return 0;
}

I'm 9/10 sure I'm just doing something wrong in my main function, but I obviously can't see what it is. Help?

Recommended Answers

All 7 Replies

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

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.

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 ===|

I just want to ask for comparison's sake, but what compiler are you using?

I'm currently using Code::Blocks on Windows XP (too poor for new Windows, but that won't be too long in coming). Free, and brain-dead setup.

Try going to a prompt and compiling it as g++ exercise9.4.cpp exercise9.4main.cpp -o ex94 substitute your own filenames and exe file name. Somehow I think your project in C::B is not set up correctly (I don't use it so I don't know how to remedy it). You may be trying to compile your main.cpp first and it doesn't have the other code to link in.

Try going to a prompt and compiling it as g++ exercise9.4.cpp exercise9.4main.cpp -o ex94 substitute your own filenames and exe file name. Somehow I think your project in C::B is not set up correctly (I don't use it so I don't know how to remedy it). You may be trying to compile your main.cpp first and it doesn't have the other code to link in.

Ah, that was it. Code::Blocks was, for some reason, not recognizing my functions file as actual code. Cut, delete, new project file, paste, ta-dah.

Now, my functions absolutely aren't doing what they ought to, but it's back into territory I know how to blunder my way through.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.