I am trying to write a program for my class and ran into these errors. I don't know where the issue is and was wondering if any one could help. The project consists of a header, implementation, and source file. These are the errors that I am getting:

1>DivSales.obj : error LNK2005: "private: static double DivSales::totalCorpSales" (?totalCorpSales@DivSales@@0NA) already defined in Corporate Sales.obj
1>C:\Users\owner\Documents\Visual Studio 2010\Projects\CISS 243\Week 1 Assignment 2 Corporate Sales\Debug\Week 1 Assignment 2 Corporate Sales.exe : fatal error LNK1169: one or more multiply defined symbols found

Here is the .h file

private:
    double quarterSales[4];
    static double totalCorpSales;

public: 
    //Constructors
    DivSales();
    DivSales(double);

    //Mutators
    void quarterlySales(double, double, double, double);
    void setQuarterSales(double, int);

    //Accessors
    double getQuarterSales(int);

};

double DivSales::totalCorpSales = 0;

#endif

The implementation file is this:

#include <iostream>
#include "DivSales.h"

using namespace std;

//constructors
DivSales::DivSales()
{
    for(int i = 0; i < 4; i++)
    {
        quarterSales[i] = 0;
    }
}

DivSales::DivSales(double corpSales)
{
    for(int i = 0; i < 4; i++)
    {
        quarterSales[i] = 0;
    }

    totalCorpSales = corpSales;
}

//Accessors
double DivSales::getQuarterSales(int quarter)
{
    while(quarter < 0 || quarter > 3)
    {
        cout << "Quarter entered is not a valid selection. Please\n";
        cout << "enter a new quarter(1-4): ";
        cin >> quarter;
    }

    return quarterSales[quarter];
}


//Mutators
void DivSales::quarterlySales(double q1, double q2, double q3, double q4)
{
    quarterSales[0] = q1;
    quarterSales[1] = q2;
    quarterSales[2] = q3;
    quarterSales[3] = q4;

    totalCorpSales = q1 + q2 + q3 + q4;

}

void DivSales::setQuarterSales(double sales, int qtr)
{
    quarterSales[qtr] = sales;

    totalCorpSales += sales;
}

The Source file is as follows:

#include <iostream>
#include <cstdlib>
#include "DivSales.h"

using namespace std;

//Function Prototypes
void mainMenu();
void GetTotalSales();
void SetSales();

//Global Variables
const int SIZE = 6;
const int QTR = 4;
DivSales divisions[SIZE];

int main()
{
    cout << "Welcome to the Corporate Sales Tracking Program." << endl;

    mainMenu();
}

void mainMenu()
{
    char choice;
    do
    {
    cout << left << "1) Add Quarterly Sales\n" << "2) Get Sales Data\n" << "3) Exit" << endl;
    cout << "Selection: ";
    cin >> choice;
    }
    while(choice < '1' || choice > '3' || isalpha(choice));  //check user choice validity

    switch(choice)
    {
    case ('1'):
        SetSales();

    case ('2'):
        GetTotalSales();

    case ('3'):
        exit(0);    
    }
}

void SetSales()
{
    double tempSales;
    for(int d = 0; d < SIZE; d++)
    {
        for(int q = 0; q < QTR; q++)
        {
            cout << "Enter the Division " << d + 1 << " sales for quarter " << q + 1 << ": ";
            cin >> tempSales;
                while(tempSales < 0)
                {
                    cout << "Please enter a sales value above 0: ";
                    cin >> tempSales;
                }
                divisions[d].setQuarterSales(tempSales, q);
        }
    }

}

void GetTotalSales()
{
    int qtr, div;
    cout << "Which division would you like to get: ";
    cin >> div;
    while(div < 1 || div > 6)
    {
        cout << "That is not a valid selection. Please\n";
        cout << "choose a division(1-6): ";
        cin >> div;
    }
    div -= 1;
    cout << "Which quarter would you like to get: ";
    cin >> qtr;
    while(qtr < 1 || qtr > 4)
    {
        cout << "That is not a valid selection. Please\n";
        cout << "choose a quarter(1-4): ";
        cin >> qtr;
    }
    qtr -= 1;

    cout << "The sales for division " <<  div + 1 << " quarter " << qtr +1 << " is: $" << divisions[div].getQuarterSales(qtr);
}
This article has been dead for over six months. Start a new discussion instead.