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

multiple definition of functions in c++ project

本文关键字:定义 函数 项目 c++      更新时间:2023-10-16

我有一个这样的程序:

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

实现ja.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,浮动)"

有人知道它怎么了吗?

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

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