Split的多个定义

时间:2009-10-21 10:15:35

标签: c++ linker-errors

也许我还是应该躺在床上。我醒来想要编程。无论如何,现在我遇到了一些令我困惑的链接器错误。你对这一切有什么看法?我希望我不会发布过多的内容。我打算发一条,但感觉不对。我检查了错误中提到的一些头文件,但我没有看到任何地方的Split。奇怪的是,它开始命名为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);
}

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

多个目标文件正在为LCD::Split提供实施。这是因为定义在头文件中。

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

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

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