0

My c++ program fails with below reasons:

Error   1   error LNK2005: "public: void __thiscall dataHolder::addData(int)" (?addData@dataHolder@@QAEXH@Z) already defined in Source.obj
Error   2   error LNK2005: "public: void __thiscall dataHolder::findUnique(void)" (?findUnique@dataHolder@@QAEXXZ) already defined in Source.obj
Error   3   error LNK2005: "public: void __thiscall dataHolder::printData(void)" (?printData@dataHolder@@QAEXXZ) already defined in Source.obj  
Error   4   error LNK1169: one or more multiply defined symbols found   

However, when I moved all definitions in once source c++ file, it worked fine.

Below is my source code:

source.cpp file:

#include "dataClass.h"

void main(){

    dataHolder v1;

    for(int i=1;i<=10;i++)
    v1.addData(i);
    for(int i=1;i<=5;i++)
    v1.addData(i);

    v1.printData();

    v1.findUnique();
}

dataClass.h file:

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <vector>
#include <map>


using namespace std;

class dataHolder{

public:

    void printData();
    void addData(int data);

    void findUnique();

    int uniqueData();

private:
    vector<int> dataVector;
};


void dataHolder::addData(int val){
    dataVector.push_back(val);
};

void dataHolder::printData(){

    vector<int>::iterator vIt;

    for(vIt=dataVector.begin();vIt<dataVector.end();vIt++){
        cout<< *vIt<<endl;
    };
};


void dataHolder::findUnique(){
    map<int ,int> dataMap;

    vector<int>::iterator vIt;
    map<int ,int>::iterator mIt;

    for(vIt=dataVector.begin();vIt<dataVector.end();vIt++){

        if(dataMap.find(*vIt)==dataMap.end())
            dataMap[*vIt]=1;
        else
            dataMap[*vIt] = dataMap[*vIt]+1;
    };

    for(mIt=dataMap.begin();mIt != dataMap.end();mIt++){
        if(mIt -> second == 1)
            cout<<mIt->first<<" is Unique"<<endl;
    };
};

please guide for the issue.

Keugyeol
  • 2,355
  • 1
  • 28
  • 36
user3440629
  • 198
  • 9
  • Unrelated tip: In general, you should avoid putting using directives in your header files. – genisage Jul 17 '14 at 06:48
  • `However, when I moved all definitions in once source c++ file, it worked fine.` So why not just do that then? – Shadow Jul 17 '14 at 07:36

4 Answers4

3

The problem is that you define the dataHolder member functions in the header file. That means that every source file including your header file will have the definition of those member functions.

You can do that, but then you have to mark them as inline, or possibly static.

What you should do is make another source file, which contains the member function definitions, and include that source file in your project so it's compiled and linked with.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Move your implementations for the class methods into a cpp file (not the header file). This way, they don't get included multiple times.

ben
  • 341
  • 1
  • 4
0

You just should move class members implementation to .cpp file. For example dataClass.cpp:

#include "dataClass.h"

void dataHolder::addData(int val){
    dataVector.push_back(val);
};

void dataHolder::printData(){

    vector<int>::iterator vIt;

    for(vIt=dataVector.begin();vIt<dataVector.end();vIt++){
        cout<< *vIt<<endl;
    };
};


void dataHolder::findUnique(){
    map<int ,int> dataMap;

    vector<int>::iterator vIt;
    map<int ,int>::iterator mIt;

    for(vIt=dataVector.begin();vIt<dataVector.end();vIt++){

        if(dataMap.find(*vIt)==dataMap.end())
            dataMap[*vIt]=1;
        else
            dataMap[*vIt] = dataMap[*vIt]+1;
    };

    for(mIt=dataMap.begin();mIt != dataMap.end();mIt++){
        if(mIt -> second == 1)
            cout<<mIt->first<<" is Unique"<<endl;
    };


};
RSATom
  • 817
  • 6
  • 16
0

You could also use header guards to ensure that your header file is loaded only once.

#ifndef _DATA_CLASS_H_
#define _DATA_CLASS_H_

#include <...>

class dataHolder {
    ...
}

/* Function Definitions */

#endif /* _DATA_CLASS_H_ */

See this question: C++ - header guards

Community
  • 1
  • 1