重载函数、重新定义、C2371和C2556 C++

Overloaded functions, redefinitions, C2371 and C2556 C++

本文关键字:C2371 C2556 C++ 定义 函数 新定义 重载      更新时间:2023-10-16

好的,所以我有3个文件:

definitions.h,其中包含

#ifndef COMPLEX_H 
#define COMPLEX_H 
class Complex
{
char type; //polar or rectangular
double real; //real value 
double imaginary; //imaginary value
double length; //length if polar
double angle; //angle if polar
 public:
//constructors
Complex();
~Complex();
void setLength(double lgth){ length=lgth;}
void setAngle(double agl){ angle=agl;}
double topolar(double rl, double img, double lgth, double agl);
#endif

functions.cpp,包含

#include "Class definitions.h"
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
using namespace std;
Complex::topolar(double rl, double img, double lgth, double agl)
{
real=rl;
imaginary=img;  
lgth = sqrt(pow(real,2)+pow(imaginary,2));
agl = atan(imaginary/real);
Complex::setLength(lgth);
Complex::setAngle(agl);
return rl;
return img;
return lgth;
return agl;
}

主程序包括:

#include "Class definitions.h"
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <string.h>
#include <cmath>
#include <vector>
using namespace std;
int main(){
vector<Complex> v;
Complex *c1;
double a,b,d=0,e=0;
c1=new Complex;
v.push_back(*c1);
v[count].topolar(a,b,d,e);

但我不断得到错误C2371:重新定义;不同的基本类型和C2556:重载函数仅因返回类型而不同

我在网上找到的所有东西都说要确保function.cpp文件不包含在main中,但由于我没有犯那个错误,我的想法已经用完了,尤其是看到我所有其他以相同方式设置的函数(有单独的定义和声明)都能工作。

任何帮助都会很棒!谢谢Hx

As声明的拓扑函数应该返回double,但functions.cpp中的定义并没有说明

Complex::topolar(double rl, double img, double lgth, double agl)
{

尝试将其更改为

double Complex::topolar(double rl, double img, double lgth, double agl)
{

您的topolar函数被定义为返回double,但实现没有返回类型。我不确定这是否是错误,但它肯定是的错误。你需要

double Complex::topolar(double rl, double img, double lgth, double agl)

在实施过程中。

此外,您在实现中似乎有许多返回语句。这也是一个错误。只有第一个会起作用:

return rl; // function returns here. The following returns are never reached.
return img;
return lgth;
return agl;