gpt4 book ai didi

c++ - 比较字符串的模板

转载 作者:行者123 更新时间:2023-11-28 07:40:01 26 4
gpt4 key购买 nike

我正在尝试编写我自己的比较字符串的模板,但我无法弄清楚它是如何工作的,尽管我已经学习了一些教程。我在编译期间仍然遇到此错误,我不知道如何解决: unreference to CSearch<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::equal_to<char> >::Add(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

这是我目前所拥有的:

template <typename _Type, typename _Comparator = std::equal_to<typename _Type::value_type> >
class CSearch {
public:
// default constructor

CSearch() : cmp(_Comparator()) {
};
// constructor with comparator parameter

CSearch(const _Comparator &_cmp) : cmp(_cmp) {
};

// destructor (if needed)

~CSearch() {

};

void Add(int id,
const _Type & needle);
set<int> Search(const _Type & hayHeap) const;

_Comparator cmp;

private:
// empty copy constructor
// empty operator =
};



class CharComparator {
public:

CharComparator(bool caseSensitive = true)
: m_CaseSensitive(caseSensitive) {
}

bool operator () (const char & a, const char & b) const {
return m_CaseSensitive ? a == b : toupper(a) == toupper(b);
}
private:
bool m_CaseSensitive;
};

int main(int argc, char** argv) {
CSearch <string> test1;
test1 . Add(0, "hello");
test1 . Add(1, "world");
test1 . Add(2, "rld");
test1 . Add(3, "ell");
test1 . Add(4, "hell");
return 0;

CSearch <string, bool (*)(const char &, const char &)> test2 ( upperCaseCompare );
test2 . Add ( 0, "hello" );
test2 . Add ( 1, "world" );
test2 . Add ( 2, "rld" );
test2 . Add ( 3, "ell" );
test2 . Add ( 4, "hell" );
printSet ( test2 . Search ( "HeLlO WoRlD!" ) );
// 0, 1, 2, 3, 4
printSet ( test2 . Search ( "hallo world!" ) );
// 1, 2

CSearch <string, CharComparator> test3 ( CharComparator ( false ) );
test3 . Add ( 0, "hello" );
test3 . Add ( 1, "world" );
test3 . Add ( 2, "rld" );
test3 . Add ( 3, "ell" );
test3 . Add ( 4, "hell" );
printSet ( test3 . Search ( "heLLo world!" ) );
// 0, 1, 2, 3, 4
printSet ( test3 . Search ( "Well, templates are hell" ) );
// 3, 4

CSearch <vector<int> > test4;
static const int needleA [] = { 1, 6, 1, 6, 9, 12 };
static const int needleB [] = { 9, 12, 7 };
static const int hayHeap [] = { 1, 6, 1, 6, 1, 6, 9, 12, 8 };
test4 . Add ( 0, makeVector ( needleA ) );
test4 . Add ( 1, makeVector ( needleB ) );
printSet ( test4 . Search ( makeVector ( hayHeap ) ) );
// 0

CSearch <vector<string> > test5;
static const string needleX [] = { "Prague", "Bern", "Rome" };
static const string needleY [] = { "London", "Prague", "Bern" };
static const string needleZ [] = { "London", "Rome" };
static const string cityHeap [] = { "Berlin", "London", "Prague", "Bern", "Rome", "Moscow" };
test5 . Add ( 0, makeVector ( needleX ) );
test5 . Add ( 1, makeVector ( needleY ) );
test5 . Add ( 2, makeVector ( needleZ ) );
printSet ( test5 . Search ( makeVector ( cityHeap ) ) );
// 0, 1
}

您能告诉我如何定义该模板以处理我在 main 中声明的所有类型吗? ?

最佳答案

这是您错误的重要部分。

Undefined reference to CSearch<...>::Add(int, std::basic_string<...> ) const&

这意味着您的代码引用CSearch中的某个函数看起来像这样:

void Add(int id, const _Type & needle);

但是那个函数似乎没有被定义

如果您在其他源文件中定义了,move it to the heade

关于c++ - 比较字符串的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16024735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com