0

也许我应该还在床上。我醒来想编程。无论如何,现在我遇到了一些令我困惑的链接器错误。你怎么看这一切?我希望我不会发布太多。我本来打算只发一篇,但感觉不对。我检查了错误中提到的一些头文件,但我没有在任何地方看到拆分。奇怪的是,它开始命名为 split,但我遇到了类似的错误。

/home/starlon/Projects/LCDControl/WidgetIcon.h:59: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
QtDisplay.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/new:101: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
DrvQt.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/stl_deque.h:79: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
LCDText.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/new:101: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
Property.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/usr/include/QtCore/qatomic_i386.h:125: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
moc_QtDisplay.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/home/starlon/Projects/LCDControl/WidgetIcon.h:59: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here

这是拆分:

std::vector<std::string> Split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    return elems; //Split(s, delim, elems);
}
4

2 回答 2

4

像这样的多个定义错误的常见原因是当您在没有 inline 关键字的头文件中定义函数时。此外,如果您发布的拆分功能来自 LCD 类,则签名缺少该LCD::部分。

于 2009-10-21T10:21:40.033 回答
2

不止一个目标文件为LCD::Split. 这是因为定义在头文件中。

内联或使函数静态,将限制使用它的每个目标文件的可见性,并防止冲突。但是,这意味着每个目标文件都将包含该函数的实现(如果使用)。此外,内联将增加每个调用点生成的二进制文件的数量。将函数设为静态,将为每个目标文件创建一个 LCD::Split 副本。

最好的解决方案是将 , 的实现放在LCD::Split自己的源文件中,以便编译后实现存在于单个目标文件中。

此外,您可能希望确保您的标头包含一个包含保护,例如#pragma once,以防止多个声明的编译时冲突。

于 2009-10-21T10:56:02.313 回答