0

Ok so I've got 3 files:

definitions.h which contains

#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 which contains

#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;

}

and the main programme contains:

#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);

But the I keep getting error C2371: redefinition; different basic types and C2556: overloaded function differes only by return type

everything i have found online says to make sure the function.cpp file isnt included in the main but as I haven't made that mistake I'm running out of ideas, especially seeing as all my other functions that are set up in the same way (with seperate definition and declaration) do work.

Any help would be great! Thanks H x

4

2 回答 2

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)
{
于 2012-03-22T14:31:46.297 回答
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;
于 2012-03-22T14:32:14.810 回答