Scott
Scott

Reputation: 1222

Unable to separate header & cpp file (error and code included)

So here are the files in my code. It compiles fine if I comment out all of the data.cpp and paste the functions inside the data.h header file under the class. But when I do it so it's separate I get this error.

duplicate symbol _salesData in:.../SalesData.build/Objects-normal/x86_64/main.o
.../SalesData.build/Objects-normal/x86_64/Data.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)" 

So I remove the #include "data.h" on main.cpp and comment out the function calls and it compiles but it obviously doesn't do what I want it to do then. I don't understand how I have duplicate symbols. Here are my files...

//data.h
#ifndef __SalesData__Data__
#define __SalesData__Data__

#include <iostream>
#include <stdlib.h>

class Data {
public:
    //[0] = day DD, [1] = month MM, [2] = year YYYY
    float data[32][13][9999];
    void newData();
    void editData();
    float viewData();
}salesData;

#endif /* defined(__SalesData__Data__) */

...

//data.cpp
#include "Data.h"

void Data::editData() {
    unsigned short day;
    unsigned short month;
    unsigned short year;
    float dailySales;
    printf("\nSelect Day: ");
    printf("\n>> ");
    std::cin >> day;
    printf("\nSelect Month: ");
    printf("\n>> ");
    std::cin >> month;
    printf("\nSelect Year: ");
    printf("\n>> ");
    std::cin >> year;
    printf("\nEnter Daily Sales: ");
    printf("\n>> ");
    if ((data[day][month][year])) {
        std::cin >> dailySales;
        data[day][month][year] = dailySales;
    }
    else {
        printf("\nSales have not been recorded for this date. If you wish to input type 'yes' else press any other key");
        std::string choice;
        std::cin >> choice;
        if (choice == "yes") {
            newData();
        }
    }
}
//... same for other class functions

...

//main.cpp
#include <iostream>
#include <stdlib.h>
#include "Data.h"

void selectMain() {

    char choice;
    while (1) {
        printf("Please select an option.");
        printf("\n1. Input Data");
        printf("\n2. View Data");
        printf("\n3. Edit Data");
        printf("\n>> ");
        std::cin >> choice;
        switch (choice) {
            case '1':
                salesData.newData();
                return;
            case '2':
                salesData.viewData();
                return;
            case '3':
                salesData.editData();
                return;
            default:
                printf("\nInvalid Choice. ");
                continue;
        }
    }
}

int main(int argc, const char * argv[])
{
    char endChar;
    while (endChar != '`') {
        selectMain();
        printf("\nPress any key to continue or '`' to end the program.\n");
        std::cin >> endChar;
    }
    printf("\nProgram Ended");
    return 0;
}

Upvotes: 0

Views: 163

Answers (1)

mindo
mindo

Reputation: 139

Problem is cause the way you define variable salesData in header file, after class declaration. Because of this it gets defined both in data.cpp file and in your main.cpp file. Move the salesData variable to the main.cpp

Upvotes: 2

Related Questions