2012-03-22 102 views
0

好了,所以我有3個文件:重載函數,重新定義,C2371和C2556 C++

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:重載函數differes只有返回類型

我的一切都在網上找到說來確保包括在主要的function.cpp文件的心不是,但我還沒有犯下這個錯誤我跑出於想法,尤其是看到我所有以同樣方式建立的其他功能(具有單獨的定義和聲明)都可以工作。

任何幫助將是偉大的! 感謝 ^ h X

+0

什麼是重新定義?錯誤信息的實際內容是什麼?當代碼中沒有任何代碼時,爲什麼要問過載?爲什麼你在同一個函數中有4個return語句?爲什麼將局部變量作爲參數傳遞給極座標函數?主函數中'count'的值是多少?右括號在哪裏?你爲什麼使用new分配局部變量?這裏有很多問題。 – 2012-03-22 14:33:13

+1

定義中是否聲明瞭該類.h應該保持未關閉狀態? – milliburn 2012-03-22 14:36:25

回答

2

所申報topolar函數將返回雙,但在functions.cpp定義不說,

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

嘗試這種更改爲

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

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;