重载函数,重定义,C2371和C2556 C ++

时间:2012-03-22 14:18:58

标签: c++ overloading redefinition

好的,我有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中,但由于我没有犯这个错误,我的想法已经用完了,特别是看到我设置的所有其他功能以相同的方式(使用单独的定义和声明)起作用。

任何帮助都会很棒! 谢谢 H X

2 个答案:

答案 0 :(得分:2)

由于声明的topolar函数应返回double,但functions.cpp中的定义并未表示

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

尝试将此更改为

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

答案 1 :(得分:2)

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

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

在实施中。

此外,您似乎在实现中有许多return语句。这也是一个错误。只有第一个才有效:

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