0

I'm trying to write my own template for comparing strings, but I can't figure out, how it shall be working, although I've walked through a few tutorials. I'm still getting this error during compilation and I have no idea, how to fix it: 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&)

This is what I have so far:

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
}

Could you please show me, how to define that template to be able to handle all types that I've declared in main?

Martin Dvoracek
  • 1,714
  • 6
  • 27
  • 55
  • 3
    Please complete the error message. That doesn't tell us what the message is. Also, you should not use _ followed by a cap for your identifiers. These are reserved for use by the system, and you can get undefined behavior from doing this. – metal Apr 15 '13 at 21:12
  • post updated.. Ok, thanks for advice, I'll rename my variables, nevertheless I don't think, that this is causing the problem right now – Martin Dvoracek Apr 15 '13 at 21:16
  • Are the implementations of `Add` and `Search` member functions in a different *.cpp* file? If so, that's your problem. The compiler needs to be able to *see* the code for class and function templates at the callsite. Also, you don't need to add semicolons after the bodies of member functions. – Praetorian Apr 15 '13 at 21:18
  • 1
    Have you **defined** your `Add` method? – SomeWittyUsername Apr 15 '13 at 21:18
  • No they aren't. I'll implement them later. My problem is not the inner logic of this program. The problem is the template issue and how to make this whole stuff to work... Until then can be `Add` and `Search` functions empty :) – Martin Dvoracek Apr 15 '13 at 21:22
  • @Dworza Defining them would resolve the error you're getting. Make them empty functions `{ }` for now if you want to implement them later. – Drew Dormann Apr 15 '13 at 21:24
  • 1
    @Dworza No-one cares (least of all the compiler) whether your function definitions are empty or not. The issue is whether you have defined them, and if you have then where you have defined them. – john Apr 15 '13 at 21:24
  • Empty or not is unrelated. The build will fail if the linker can't find the function object - that's exactly what will happen if you do not define the function. – SomeWittyUsername Apr 15 '13 at 21:25
  • 1
    `_Comparator` is a reserved identifier. You cannot start an identifier with underscore followed by a capital letter, and you can start with an underscore followed by a lower case letter only in some contexts... it is better to avoid leading underscores – David Rodríguez - dribeas Apr 15 '13 at 21:41

1 Answers1

1

Here's the important part of your error.

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

It means that your code refers to some function in CSearch that looks like this:

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

But that function doesn't appear to have been defined.

If you've defined in in some other source file, move it to the header.

Community
  • 1
  • 1
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180