我读过很多这样的问题,但我不能 100% 确定我的问题是否有任何不同。我已经为正确的答案搜索了很多,但大多数答案都围绕宏观 gaurds。我有一个派生自 std::string
的自定义字符串类,后来因为我在现实世界中使用该项目,所以我不想仅仅为了扩展功能而派生 std::string 。所以我将函数从 customString.h 移到了 utils_string.h 中。这是定义:
#ifndef OPS_TOOLKIT_UTILS_STRING_H
#define OPS_TOOLKIT_UTILS_STRING_H
#include <string>
#include <algorithm>
#include <vector>
namespace utils{
namespace string{
const std::string kilikeDelimiter = "%";
void tolower(std::string& str){
//CODE
}
bool iequals(const std::string& str1,const std::string& str2){
//CODE
}
bool ilike(const std::string& str,const std::string& pattern){
//CODE
}
std::string prependAndAppendILikeDelimiter(const std::string& str) {
//CODE
}
} //namespace string
} //namespace utils
#endif
我收到关于 ...first defined here
的投诉这是编译器的准确输出。注意:这里没有类
/tmp/ccCFVTXP.o: In function
utils::string::tolower(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)': listings.cpp:(.text+0x0): multiple definition of
utils::string::tolower(std::basic_string, std::allocator >&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x0): first defined here /tmp/ccCFVTXP.o: In functionutils::string::iequals(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': listings.cpp:(.text+0x55): multiple definition of
utils::string::iequals(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x55): first defined here /tmp/ccCFVTXP.o: In functionutils::string::ilike(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': listings.cpp:(.text+0x118): multiple definition of
utils::string::ilike(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x118): first defined here /tmp/ccCFVTXP.o: In functionutils::string::prependAndAppendILikeDelimiter(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': listings.cpp:(.text+0x484): multiple definition of
utils::string::prependAndAppendILikeDelimiter(std::basic_string, std::allocator > const&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x484): first defined here /tmp/ccifZISK.o: In functionutils::string::tolower(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)': match_count.cpp:(.text+0x0): multiple definition of
utils::string::tolower(std::basic_string, std::allocator >&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x0): first defined here /tmp/ccifZISK.o: In functionutils::string::iequals(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': match_count.cpp:(.text+0x55): multiple definition of
utils::string::iequals(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x55): first defined here /tmp/ccifZISK.o: In functionutils::string::ilike(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': match_count.cpp:(.text+0x118): multiple definition of
utils::string::ilike(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x118): first defined here /tmp/ccifZISK.o: In functionutils::string::prependAndAppendILikeDelimiter(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': match_count.cpp:(.text+0x484): multiple definition of
utils::string::prependAndAppendILikeDelimiter(std::basic_string, std::allocator > const&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x484): first defined here collect2: ld returned 1 exit status
我很抱歉弄得一团糟,但我不想错过任何遇到过类似问题的人。
还有一件事;它说 match_count.cpp *listing.cpp* 和 listings.cpp 它只是使用::utils::string 中定义的函数: :函数()
最佳答案
头文件不能定义代码,除非它是显式内联
。否则(因为预处理器像一个巨大的复制/粘贴机器一样工作)就好像为每个执行 #include
的文件定义一次函数。
因此,将 inline
添加到其中的每一个,或者将它们放在单个 .cpp
中,但不要放在标题中。
关于c++ - 首先在 g++ 中从类到命名空间定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11514771/