c++ 中的不同声明和定义
我对声明与定义的规则有点模糊。
我在 funcs.h 中有以下声明:
void sumTotalEnrgyAndClush(Protein &A,Protein &B,double ans[2],double enrgyA[18][18],double enrgyB[18][18]);
请注意,ans[2] 位于 enrgyA 和 B 之前。
在 funcs.cpp 文件中,定义如下所示:
void sumTotalEnrgyAndClush(Protein &A,Protein &B,double enrgyA[18][18],double enrgyB[18][18],double ans[2])
它编译(通过 makefile)并且工作正常。
我还注意到,如果删除声明,编译器似乎可以很好地管理。
为什么参数顺序的改变并不重要?是不是最后 3 项都是指针,所以顺序的差异并不重要?
I'm a bit hazy on the rules of declarations vs. definitions.
I have the following declaration in funcs.h:
void sumTotalEnrgyAndClush(Protein &A,Protein &B,double ans[2],double enrgyA[18][18],double enrgyB[18][18]);
Notice that ans[2] is before enrgyA and B.
In the funcs.cpp file the definition starts like this:
void sumTotalEnrgyAndClush(Protein &A,Protein &B,double enrgyA[18][18],double enrgyB[18][18],double ans[2])
It compiles (via makefile) and works fine.
I also noticed that if I remove the declaration the compiler seems to manage just fine.
Why doesn't the change in the order of the arguments matter? Is it that the last 3 items are all pointers so the difference in order doesn't matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顺序确实很重要。在 C++ 中,函数可以重载,因此如果两个或多个函数具有不同的参数(或者,如果它们是成员函数,则它们的 const 限定不同),则它们可以具有相同的名称。
您实际上声明了两个
sumTotalEnrgyAndClush
函数。头文件中的声明声明了一个从未定义的函数,而源文件中的声明声明并定义了第二个函数。如果您尝试使用头文件中声明的函数(例如,通过调用它或获取其地址),您将收到错误,因为该函数未定义。
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).
You have actually declared two
sumTotalEnrgyAndClush
functions. The declaration in the header file declares one function that is never defined, and the declaration in the source file declares and defines a second function.If you tried to use the function declared in the header file (by calling it or taking its address, for example), you would get an error because that function is not defined.