c++ - 首先在 g++ 中从类到命名空间定义

标签 c++ class namespaces g++ multiple-definition-error

我读过很多这样的问题,但我不能 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 ofutils::string::tolower(std::basic_string, std::allocator >&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x0): first defined here /tmp/ccCFVTXP.o: In function utils::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 ofutils::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 function utils::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 ofutils::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 function utils::string::prependAndAppendILikeDelimiter(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': listings.cpp:(.text+0x484): multiple definition ofutils::string::prependAndAppendILikeDelimiter(std::basic_string, std::allocator > const&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x484): first defined here /tmp/ccifZISK.o: In function utils::string::tolower(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)': match_count.cpp:(.text+0x0): multiple definition ofutils::string::tolower(std::basic_string, std::allocator >&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x0): first defined here /tmp/ccifZISK.o: In function utils::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 ofutils::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 function utils::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 ofutils::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 function utils::string::prependAndAppendILikeDelimiter(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': match_count.cpp:(.text+0x484): multiple definition ofutils::string::prependAndAppendILikeDelimiter(std::basic_string, std::allocator > const&)' /tmp/cczDtRdT.o:listing.cpp:(.text+0x484): first defined here collect2: ld returned 1 exit status

我很抱歉弄得一团糟,但我不想错过任何遇到过类似问题的人。

还有一件事;它说 ma​​tch_count.cpp *listing.cpp* 和 listings.cpp 它只是使用::utils::string 中定义的函数: :函数()

最佳答案

头文件不能定义代码,除非它是显式内联。否则(因为预处理器像一个巨大的复制/粘贴机器一样工作)就好像为每个执行 #include 的文件定义一次函数。

因此,将 inline 添加到其中的每一个,或者将它们放在单个 .cpp 中,但不要放在标题中。

关于c++ - 首先在 g++ 中从类到命名空间定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11514771/

相关文章:

c++ - SetWindowsHookEx(WH_KEYBOARD) 不使用线程 ID

c++ - C++ 中 SetProcessAffinityMask 的示例用法?

c++ - 无序映射相等功能 c++

c++ - Qt eventfilter 不检测 objectName

python - 优化类模拟代码?

php - 将导入的类名转换为完全限定的类名

c++ - 如何在基类中调用派生类的函数?

javascript - jQuery - 如何创建具有相同类名但不同编号的变量和单击事件

php - fatal error : Namespace declaration statement has to be the very first statement in the script

ruby-on-rails-3 - 将模型移动到 gem 中 - Rails 4.1