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

发布于 2024-09-16 17:55:03 字数 524 浏览 8 评论 0原文

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

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-09-23 17:55:03

为什么参数顺序的改变并不重要?

顺序确实很重要。在 C++ 中,函数可以重载,因此如果两个或多个函数具有不同的参数(或者,如果它们是成员函数,则它们的 const 限定不同),则它们可以具有相同的名称。

您实际上声明了两个 sumTotalEnrgyAndClush 函数。头文件中的声明声明了一个从未定义的函数,而源文件中的声明声明并定义了第二个函数。

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

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文