简体   繁体   English

C ++多重定义

[英]C++ Multiple definition

So I have a header file which has function implementations in it as well as prototypes. 所以我有一个头文件,其中包含函数实现以及原型。 I am now trying to separate these implementations from the file and just leave the prototypes since I know its not good standards to have the implementations in header files. 我现在尝试将这些实现与文件分离,只保留原型,因为我知道在头文件中包含实现不是很好的标准。 However when I remove the implementations from the header file and put them in their own cpp file and then try to link that cpp file to the rest of the project it complains about multiple definitions of the functions. 但是,当我从头文件中删除实现并将它们放在自己的cpp文件中,然后尝试将该cpp文件链接到项目的其余部分时,它会抱怨函数的多个定义。 Thanks for any help/advice 感谢您的帮助/建议

std::bitset<LENGTH>  foo1(const std::string stringVal);
std::bitset<LENGTH>  foo2(const int decimalVal);
std::string          foo3(const int integerVal);

I have an include guard on so it doesn't get included more than once as well. 我有一个包含卫士,所以它不会被包含多次。

Then in the cpp I simply have my implementations for these functions just straightforward. 然后在cpp中,我只是简单地实现了这些功能的实现。 Yet it complains of multiple definitions of the functions if they are separate from the header file. 但是,如果它们与头文件分开,则会抱怨函数的多个定义。

EDIT: Stupid mistake on my part this is solved. 编辑:我这方面的愚蠢错误已解决。

That is just a declaration. 那只是一个声明。 Even if the file didn't have include guards, it wouldn't give that error. 即使文件没有包含防护,也不会出现该错误。

Multiple definition usually means you're defining the function in more than one implementation file. 多重定义通常意味着您要在多个实现文件中定义函数。

Here's a checklist: 这是一个清单:

  • make sure the implementation is definetely outside of the header. 确保实现完全在标头之外。

  • make sure only one source file defines the function 确保只有一个源文件定义了该功能

  • make sure you don't include the source file (this one is trivial, I hope you know not to do this) 确保您不包括源文件(这是微不足道的,希望您知道不要这样做)

  • finally, check that you run a clean build 最后,检查您运行的是干净构建

I guess you're not compiling the other cpp file that previously included the header file. 我猜您不是在编译先前包含头文件的其他cpp文件。 So the object file still contains the definitions. 因此,目标文件仍然包含定义。 BTW, not having implementations in header files isn't at all about good standards. 顺便说一句,在头文件中没有实现完全不是关于好的标准。 It's a must, so that you can include the same header in more than one compilation unit. 这是必须的,因此您可以在多个编译单元中包含相同的标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM