错误 5 错误 C2535 成员函数已定义或声明.视觉工作室 2013.
Error 5 error C2535 member function already defined or declared. VisualStudio 2013
我在这段代码上遇到了一些问题,我的Visual Studio 2013通知此错误:"错误5错误C2535成员函数已定义或声明"。我在代码上标记了它发生的确切位置。
#ifndef __NORMAL_H_INCLUDED__
#define __NORMAL_H_INCLUDED__
class Normal{
public:
double x;
double y;
double z;
Normal(double x, double y, double z);
Normal(double x, double y);
Normal();
};
#endif
#include "Normal.h"
Normal::Normal(double x=0, double y=0, double z=0){
this->x = x;
this->y = y;
this->z = z;
}
Normal::Normal(double x=0, double y=0){
this->x = x;
this->y = y;
this->z = 0;
} // ERROR HERE = Error 5 error C2535 member function already defined or declared
Normal::Normal(){
x=0;
y =0 ;
z =0;
}
使用 clang++ 编译代码会给出此错误:
normal.cpp:12:23: error: addition of default argument on redeclaration makes this
constructor a default constructor
Normal::Normal(double x=0, double y=0, double z=0){
^ ~
normal.cpp:7:5: note: previous declaration is here
Normal(double x, double y, double z);
^
normal.cpp:19:23: error: addition of default argument on redeclaration makes this
constructor a default constructor
Normal::Normal(double x=0, double y=0){
^ ~
normal.cpp:8:5: note: previous declaration is here
Normal(double x, double y);
这可能是一个好的说法。编译器无法区分:
Normal n = Normal();
和
Normal m = Normal(0, 0);
或
Normal o = Normal(0, 0, 0);
如果您始终希望参数为零填充,只需使用一种形式:
class Normal{
public:
double x;
double y;
double z;
Normal(double x = 0, double y = 0, double z = 0);
};
永远不要将默认参数放在实现部分,无论您是否在标头中都有它们,因为诱惑是在实现或标头中分别更改它们,然后你会得到非常混乱的结果。
当然,您可能要考虑使用两个或三个不同版本的构造函数,而不是默认值。但是从中选择一个。或者使用具有一个"必须具有"值和一个"未指定值"的构造函数。传递参数当然会在调用代码中添加更多代码,因此在大型项目中,使用不同数量的对象多次创建Normal
对象,传递三个double
参数和不传递任何参数之间可能存在显着的大小差异。
我应该补充一点,代码在 4.8.2 版本中g++
编译良好。这可能是一个错误...
相关文章:
- 错误:未在此范围内声明'reverse'
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 错误:"imread"未在此范围内声明
- 在气泡排序程序中未声明错误功能
- 使用全局声明的向量时,C++双重释放错误/损坏
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- C++ [错误] 声明'char '隐藏参数。什么意思?
- 类中的静态函数给出了错误:声明中的两种或多个数据类型
- 错误:声明冲突 - 指针不能是 int
- 收到 CUDA 错误"声明与以前的"variable_name"不兼容
- 错误:声明不声明
- Visual Studio 2010 链接器错误C++声明我从头文件创建的类时
- Cython 错误:声明不声明任何内容
- 错误"声明非模板函数"C++
- 无效地使用不完整的类型/前向错误声明.可能误用抽象类?(c++)
- 错误:声明冲突.用c++编译c代码
- C++错误:声明中存在显式限定
- else语句(C++)上的错误w/声明
- 初始化const类成员,错误:声明没有声明任何内容