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
实现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
文件。将实现文件放在一起是链接器的工作,而不是您的工作。
要编译主程序,编译器只需要头文件中的声明。
相关文章:
- 在命名空间中定义函数还是限定函数
- 为什么在定义函数之前先声明它
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 这个c++代码是如何在没有定义函数的情况下运行的
- 具有外部"c"和程序集的未定义函数
- 已定义函数时出现 G++ "未定义的引用"错误
- 将自定义函数传递到基抽象类中以延迟执行
- C++使用 rand 定义函数语法
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 使用定义函数模板别名
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 在内联程序集中定义函数和从 C++ 调用时出现问题
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 声明和定义函数静态会产生"undefined reference to function_name()"
- 朋友定义函数的名称空间是什么
- 介子 对用户定义函数的未定义引用
- 如何使用 "using" 关键字定义函数原型/签名
- 在 C 结构中定义C++函数