error about multiply defined symbols

May 2, 2012 at 10:29pm
Many apologies if I'm just being dense. I have a header file which looks like this:

#ifndef BIGNUMBER_H
#define BIGNUMBER_H

class BigNumber
{
private:
unsigned int numDigits;
std::vector<int> theNumber;

public:
void formatCorrection();
BigNumber():numDigits(0){}
BigNumber(const std::vector<int>& aVector);
BigNumber(const unsigned int aValue);
BigNumber(BigNumber &obj);

unsigned int getDigits(){return numDigits;}

void showBigNumber();

void showBigNumber(unsigned int n);

BigNumber& operator=(const BigNumber& obj);

BigNumber& operator=(const unsigned int n);

BigNumber operator+(const BigNumber& obj);

BigNumber operator*(const BigNumber& obj);

BigNumber& operator*(const unsigned int n);
int digitSum();


int firstNdigitSum(const unsigned int n);

int lastNdigitSum(const unsigned int n);
};

#endif


and a BigNumber.cpp file which has the implementation:

#include <vector>
#include<iostream>
#include"BigNumber.h"
using namespace std;


//implementation of functions, for example

BigNumber::BigNumber(const vector<int>& aVector):numDigits(0){
vector<int>::const_reverse_iterator rit;
for(rit = aVector.rbegin();rit!=aVector.rend();rit++)
{ theNumber.push_back(*rit);
numDigits++;}
formatCorrection();}

The third file, BigNumberMain.cpp, looks like

#include <iomanip>
#include <fstream>
#include"BigNumber.cpp"

int main{
//do stuff with BigNumber
return 0;
}



If I delete the header file and put all the implementation inside the class definition in BigNumber.cpp, everything works as intended. But with the project structured as shown, when I try and run it, for each function I get an error looking like

BigNumberMain.obj : error LNK2005: "public: void __thiscall BigNumber::formatCorrection(void)" (?formatCorrection@BigNumber@@QAEXXZ) already defined in BigNumber.obj

What stupid thing have I done?
I'm using MSVC++2010 express btw.
May 2, 2012 at 11:15pm
You are including the BigNumber.cpp file in your main file. Ordinarily you shouldn't include the implementation files, just the headers, i.e. BigNumber.h
May 3, 2012 at 8:11am
Many thanks. It works now - apologies for ignorance.
Topic archived. No new replies allowed.