c ++中的不同声明和定义

Mei*_*eir 5 c++ declaration

我对声明与定义的规则有点模糊.

我在funcs.h中有以下声明:

void sumTotalEnrgyAndClush(Protein &A,Protein &B,double ans[2],double enrgyA[18][18],double     enrgyB[18][18]);
Run Code Online (Sandbox Code Playgroud)

请注意,ans [2]在enrgyA和B之前.

在funcs.cpp文件中,定义如下所示:

void sumTotalEnrgyAndClush(Protein &A,Protein &B,double enrgyA[18][18],double enrgyB[18][18],double ans[2])
Run Code Online (Sandbox Code Playgroud)

它编译(通过makefile)并正常工作.

我还注意到,如果我删除声明,编译器似乎管理得很好.

为什么参数顺序的变化不重要?难道最后3个项目都是指针,所以顺序的差异无关紧要吗?

Jam*_*lis 12

Why doesn't the change in the order of the arguments matter?

The order does matter. In C++, functions can be overloaded, so two or more functions can have the same name if they have different parameters (or, if they are member functions, if they differ in const-qualification).

你实际上已经声明了两个sumTotalEnrgyAndClush函数.头文件中的声明声明了一个从未定义过的函数,源文件中的声明声明并定义了第二个函数.

如果您尝试使用头文件中声明的函数(例如,通过调用它或获取其地址),您将收到错误,因为未定义该函数.