c ++项目中函数的多重定义

时间:2015-04-01 19:41:46

标签: c++ class definition

我有一个这样的程序:

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

我的编译器会报告如下问题:

  

“Project_1 :: Complex_n :: Complex_n的多个定义(float,   浮动)“

有人知道它有什么问题吗?

1 个答案:

答案 0 :(得分:1)

在主文件中,您不得包含实施.cpp文件。您只需要包含标头.h文件。将实现文件放在一起是链接器的工作,而不是你的工作。

要编译主程序,编译器只需要头文件中的声明。