c++项目中函数的多重定义
multiple definition of functions in c++ project
我有这样一个程序:
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
我的编译器报告了如下问题:
"multiple definitions of Project_1::Complex_n::Complex_n(float,
float)"
有人知道它有什么问题吗?
在您的主文件中,您不得包含您的实施 .cpp
文件。您只需包含 header .h
文件。将实现文件放在一起是链接器的工作,而不是你的工作。
要编译您的主程序,编译器只需要 header 文件中的声明。
我有这样一个程序:
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
我的编译器报告了如下问题:
"multiple definitions of Project_1::Complex_n::Complex_n(float, float)"
有人知道它有什么问题吗?
在您的主文件中,您不得包含您的实施 .cpp
文件。您只需包含 header .h
文件。将实现文件放在一起是链接器的工作,而不是你的工作。
要编译您的主程序,编译器只需要 header 文件中的声明。