zakho
zakho

Reputation: 13

multiple definition of functions in c++ project

I have a program like this:

zesp.h

#ifndef ZESP_H_INCLUDED
#define ZESP_H_INCLUDED
#include <iostream>

namespace Project_1
{
void error ();

class Complex_n {
private:
    float real, imag;
public:
    Complex_n (float n_real, float n_imag);
    // overloaded functions
    Complex_n & operator= (const Complex_n & a);
    Complex_n & operator+= (const Complex_n & a);
    Complex_n & operator-= (const Complex_n & a);
    Complex_n & operator*= (const Complex_n & a);
    Complex_n & operator/= (const Complex_n & a);
    Complex_n operator! ();
    // friend
    friend std::ostream & operator<< (std::ostream & s, const Complex_n & a);
    friend Complex_n operator+ (const Complex_n & a, const Complex_n & b);
    friend bool operator== (const Complex_n & a, const Complex_n & b);
    friend Complex_n operator- (const Complex_n & a, const Complex_n & b);
    friend Complex_n operator* (const Complex_n & a, const Complex_n & b);
    friend Complex_n operator/ (const Complex_n & a, const Complex_n & b);

};

std::ostream & operator<< (std::ostream & s, const Complex_n & a);
Complex_n operator+ (const Complex_n & a, const Complex_n & b);
bool operator== (const Complex_n & a, const Complex_n & b);
Complex_n operator- (const Complex_n & a, const Complex_n & b);
Complex_n operator* (const Complex_n & a, const Complex_n & b);
Complex_n operator/ (const Complex_n & a, const Complex_n & b);

}
#endif // ZESP_H_INCLUDED

implementacja.cpp

   #include "zesp.h"

    using namespace Project_1;

    // Complex_n
    // constructor
    Complex_n::Complex_n (float n_real, float n_imag = 0.0)
   {
     real = n_real;
     imag = n_imag;
   }

   // functions
  Complex_n & Complex_n::operator= (const Complex_n & a)
 {
   real = a.real;
  imag = a.imag;
  return *this;
 }

 Complex_n & Complex_n::operator+= (const Complex_n & a)
{
  real += a.real;
  imag += a.imag;
  return *this;
}

Complex_n & Complex_n::operator-= (const Complex_n & a)

{
  real = real - a.real;
  imag = imag - a.imag;
  return *this;
 }

 Complex_n & Complex_n::operator*= (const Complex_n & a)
 {
   real = (real * a.real) - (imag * a.imag);
   imag = (real * a.imag) + (imag * a.real);
   return *this;
 }

 Complex_n & Complex_n::operator/= (const Complex_n & a)
 {
   if (a.real == 0.0 && a.imag == 0.0)
   {
      error ();
      return *this;
   }
   real = (real * a.real + imag * a.imag) / (a.real * a.real + a.imag * a.imag);
imag = (imag * a.real - a.real * a.imag) / (a.real * a.real + a.imag * a.imag);
return *this;
   }

   Complex_n Complex_n::operator! ()
  {
    imag = imag * (-1.0);
    return *this;
  }

  // friend functions
  std::ostream & Project_1::operator<< (std::ostream & s, const Complex_n & a)
 {
   if (a.real != 0)
   s<<a.real;
   if (a.imag < 0)
       s<<" "<<a.imag<<"*i"<<std::endl;
   else if (a.imag > 0)
           s<<" + "<<a.imag<<"*i"<<std::endl;
   return s;
  }

  Complex_n Project_1::operator+ (const Complex_n & a, const Complex_n & b)
  {
     Complex_n temp (a.real + b.real, a.imag + b.imag);
     return temp;
  }

  bool Project_1::operator== (const Complex_n & a, const Complex_n & b)
  {
     if (a.real == b.real && a.imag == b.imag)
       return true;
     return false;
  }

  Complex_n Project_1::operator- (const Complex_n & a, const Complex_n & b)
  {
     Complex_n temp (a.real - b.real, a.imag - b.imag);
     return temp;
  }

  Complex_n Project_1::operator* (const Complex_n & a, const Complex_n & b)
 {
    Complex_n temp ((a.real * b.real) - (a.imag * b.imag), (a.real * b.imag) + (a.imag * b.real));
     return temp;
 }

 Complex_n Project_1::operator/ (const Complex_n & a, const Complex_n & b)
{
   Complex_n temp (0.0, 0.0);
   if(b.real == 0.0 && b.imag == 0.0)
   {
       error ();
       return temp;
   }
   temp.real = (a.real * b.real + a.imag * b.imag) / (b.real * b.real + b.imag * b.imag);
   temp.imag = (a.imag * b.real - b.real * b.imag) / (b.real * b.real + b.imag * b.imag);
   return temp;
  }
  // Complex_n

  void error ()
  {
      std::cout<<"Blad! Nie dziel przez 0."<<std::endl;
  }

main.cpp:

 #include "implementacja.cpp"

 using namespace Project_1;

 // some tests

And my compiler reports problems like:

"multiple definitions of Project_1::Complex_n::Complex_n(float, float)"

Does anybody know whats wrong with it?

Upvotes: 1

Views: 78

Answers (1)

lodo
lodo

Reputation: 2383

In your main file, you must not include your implementation .cpp file. You only have to include the header .h file. Putting together the implementation files is linker's work, not your work.

To compile your main program, the compiler only needs the declarations inside the header file.

Upvotes: 1

Related Questions