重载函数、重新定义、C2371和C2556 C++
Overloaded functions, redefinitions, C2371 and C2556 C++
好的,所以我有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;
相关文章:
- 在Visual Studio中重建我的项目时,我会在同一行错误C2371上遇到2个错误
- 将 Qt 5.3.1 与 Open Inventor 9.4.1 配合使用时出错 visual2013: C2371: "GLdouble redefinition"
- C++ 静态成员函数错误 C2556 重载函数仅因返回类型而异
- MSVC错误C2371 "redefinition; different basic types",除了我没有看到我在哪里重新定义任何东西
- 模板类中的静态变量导致 C2371 'redefinition; different basic types'
- 重载函数、重新定义、C2371和C2556 C++
- 无法找出导致此错误的原因:C2371
- C2371错误:重定义;不同的基本类型
- C2556:重载函数只在返回类型上不同
- 编译C dll部署MATLAB代码时出现C2371错误
- 错误 C2371"DWORD":重新定义;不同的基本类型
- 队列错误 C2371:'ItemType':重新定义;不同的基本类型