快速的问题.
我试图让C++固定下来,今天我花了几个小时的双定义链接器错误("这已经定义了!")我终于意识到这是因为我有这样的布局:
main.cpp中
#include Dog.cpp
Run Code Online (Sandbox Code Playgroud)Dog.cpp
#include Dog.h
Run Code Online (Sandbox Code Playgroud)Dog.h
// (Dog class and prototype of test function)
Run Code Online (Sandbox Code Playgroud)现在我通过在main.cpp中包含Dog.h而不是Dog.cpp来清除它.
通过包含.h文件,具有相同前缀的.cpp文件是否可以用程序编译?
当程序只包含.h并且没有任何对Dog.cpp的引用时,我感到震惊.我花了很多年的谷歌搜索,但没有答案真的帮助我理解发生了什么.
编辑:我忘了添加我在.h中的原型,并在.cpp中为类定义了函数,这就是给出了"已定义"错误的原因.