在类中声明但未定义的方法
我有一个类声明了一个方法但没有实现它。该方法不是虚函数。在对应的cpp文件中我没有找到相同方法的定义。类中声明的所有其他方法均已定义。
我编译了代码,一切顺利。我的印象是 cpp 必须强制定义已声明的方法。
如果有人能详细说明这一点,我将不胜感激。我使用的是 VS2010 的 cl 编译器。
I have Class which declares a method but does not implement it. The method is not a virtual function. In the corresponding cpp file I did not find the definition of the same method. All other methods declared in the class were defined.
I compiled the code and it went fine. I was of the impression that cpp must mandate the definition of a declared method.
Appreciate if somebody could elaborate on the same. I am using cl compiler from VS2010.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码将编译,但会出现链接错误。
构建项目的可执行文件涉及两个阶段:
在编译期间,编译器仅通过验证语言语义将源代码转换为目标代码。
在链接期间,链接器实际上查找符号的定义并从多个目标文件(在编译期间创建)创建可执行文件。
编译器分别编译每个翻译单元(.cpp + 头文件)中的源代码,因此它假设定义应该存在于其他一些源文件中。链接器尝试查找对函数定义的引用,因此链接器将报告缺少的定义。
请注意,链接器只需要链接您的程序使用的那些符号,
例如:如果您的程序声明了一个函数,但没有提供定义&那么永远不会在任何地方使用/调用该函数,链接器不需要嵌入代码来跳转到任何函数调用站点的函数目标代码所在的地址。
在这种情况下,链接器根本不需要查找函数定义。因此代码将被编译和链接。
Your code will compile but it will give linking errors.
Building an executable of your project involves two stages:
During Compilation the compiler merely translates the source code into object code by verifying the language semantics.
During Linking the linker actually looks up for the definitions of the symbols and creates an executable from multiple object files (created during compilation).
The compiler compiles the source code in each translation unit (.cpp + header files) separately and hence it assumes the definition should be present in some other source file. It is the Linker who tries to find references to the function definitions, and hence the missing definition will be reported by the linker.
Note that the linker needs to link only those symbols which are used by your program,
For ex: If your program declares a function, provides no definition & then never uses/calls the function anywhere, the linker does not need to embed the code for jumping to the address where the object code for the function resides at any function call site.
Given such a scenario the linker will simply never need to look up for the function definition at all. Hence the code will compile and link.
不需要在特定文件中实现该方法。事实上,每个方法都有一个文件以减少与库链接时的膨胀被认为是(或曾经)被认为是良好的编程实践。
这意味着,给定一个定义类的头文件(并且可能其中没有实现),编译器只能假设所有函数都在某处实现。只有在系统尝试将所有内容放在一起(链接阶段)时,您才可以明显看出您所指的是不存在的东西。
There's no requirement for the method to be implemented in a particular file. Indeed it is (or was) considered good programming practice to have one file per method to reduce bloat when linking with libraries.
This means that given a header file which defines a class (and presumably doesn't have the implementation in it), the compiler can only assume that all the functions are implemented somewhere. It's only at the point where the system attempts to put everything together (the link stage) that it becomes apparent that you are referring to something that isn't there.
这是防止分配或复制的常用技术。如果您声明它但未定义它,则在尝试使用它时会发生链接错误,即防止人们无意中使用它
It is a common technique to prevent assignment or copy. If you declare it but not define it, a linking error will occur if you try to use it i.e. prevents people using it inadvertently
编译器不会抱怨,因为没有语法/编译器错误。
链接器不会抱怨,因为您没有在程序中调用该函数,因此不需要链接它。
The compiler does not complain because there is no syntax/compiler error.
The linker does not complain because you are not calling the function in your program so it does not need to be linked.