重载函数,重新定义,C2371和C2556 C++
问题描述:
好了,所以我有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
答
所申报topolar函数将返回双,但在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;
什么是重新定义?错误信息的实际内容是什么?当代码中没有任何代码时,为什么要问过载?为什么你在同一个函数中有4个return语句?为什么将局部变量作为参数传递给极坐标函数?主函数中'count'的值是多少?右括号在哪里?你为什么使用new分配局部变量?这里有很多问题。 – 2012-03-22 14:33:13
定义中是否声明了该类.h应该保持未关闭状态? – milliburn 2012-03-22 14:36:25