Problem designing a template class?

In my CLinkedList (template) class I implemented a find function which should be accessing the

overloaded operator== function

of the Data class, which in this case happens to be CPerson,

but instead comes up withe the following compiler error:

c:\projects\templatell\templatell.cpp(95) : error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char, struct std::char_traits<char>,clas

s std::allocator<char> >' (or there is no acceptable conversion)

c:\projects\templatell\templatell.cpp(91) : while compiling class-template member function 'bool __thiscall CLinkedList<class CPerson,class std::basic_string<char,struct std::char_traits<char>,c

lass std::allocator<char> > >::find(const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class CLinkedList<class CPerson,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>

> > &)'

Here is the code I consider relevant, but if you need more, please let me know:

http://pastebin.com/f48f39f5f

(10 points for relevant answer)

Update:

cja, for some reasons you are expanding on a design, which works generally very well for me. Sorry that I could not comply with your design ethics.

But how did you answer my question? The operator overload is the problem. Just more explaination for you:

this untransparent piece of error message :

'const class std::basic_string<char, struct std::char_traits<char>,clas

s std::allocator<char> >

should reference the const string& parameter declaration of the operator== function of the Data member of the linked list. The method is the following:

bool operator==(const string& strCompare)

{

return strCompare == strLast;

}

This method in my opinion is called with the following comparison in the CLinkedList class:

bool find(const F &s, CLinkedList<T, F>& lst)

{

...

if (node->p == (const string&)s) {

...

}

So basically I need to know how to define either the operator function differently or how to make the method call compliant.

tx

Update 2:

cja, again, content wise brief on behalf of the next member you referred to in your answer.

This is supposed to be a Linked List class. So what I want to achieve is that I could take ANY Databasemember and use it as a data member in the linked list. Does this - in your opinion - still make no sense?

Update 3:

----------------------

last not least the find method with the CLinkedList parameter: I could dynamically allocate an array, or use a vector, etc. but it is not necessary. There is already a functional linked list there to store the return results of my find method.

What would be the reason to use a different data type, if I lessen code maintenance overhead by NOT introducing a new datatype?

My only objective in this context is to make the find method workable with any kind of datatype.

Update 4:

================

here is the code, which is not compilable. Under Visucal C++ 6.0 the described error message is coming up:

http://pastebin.com/f524fc1d0

Update 5:

---------------------

My solution:

CLinkedList declared a pointer to CPerson as: T *p;

So I passed a pointer to CPerson attempting to call the operator== overload. What I did not "share" was that I compared with a pointer to CPerson.

I accomplished this by defining the operator== as a friend, passing CPerson as a pointer argument. (Pls note: there are other solutions as well)

friend bool operator==(const string& strCompare, CPerson *p)

{

return strCompare == p->strLast;

}

2 Answers

Relevance
  • cja
    Lv 7
    1 decade ago
    Favourite answer

    Try this for your problematic statement:

        if (*(node->p) == s) {

    A comment on your design: I find it unusual that CLinkedList has a CLinkedList *next attribute. Normally I'd expect to find the next pointer in the node. It's almost as if you have a linked list of lists, or maybe CLinkedList is just not the right name for this class. It also seems strange to me that CLinkedList::find would need an argument that is a reference to a CLinkedList, presumably the head of the list. I would think the CLinkedList class would know where it's head is, and find could just be:

        bool find(const F &searchKey);

    You could also overload find to search for a node:

        bool find(const T &node);

    Anyway, just some things to think about. Since you only provided a fragment of your code, I can't be sure what you had in mind.

    EDIT: the point was that the left hand side of your == statement needed to be a T&, you were passing a T*. So I answered your question by suggesting a simple fix: dereferencing node->p in that statement the compiler was complaining about. You could also overload your operator== to provide a version that takes a T*, and I see that's what you did.

    As for our difference of opinion in design philosophy, you can do whatever you like, and if you have something that works, that's great. It's just that to me, having an attribute CLinkedList *next in CLinkedList is strange. It makes it seem as if you have a linked list of CLinkedList, rather than having CLinkedList be a linked list of node, your type T in the template definition. In a few of your operations you have:

        CLinkedList *node

    To me this is saying that the node is a pointer to a CLinkedList. I like to think of a linked list as having nodes, not a linked list being a node.

  • ayoub
    Lv 4
    4 years ago

    I as quickly as made a existence length Monopoly board pastime, the Iron replaced into user-friendly. So replaced into the Hat, wheelbarrow, thimble and shoe. The canine, besides the shown fact that, he replaced right into a discomfort-he stored sniffing the horses bum. And the pony bucked every time the vehicle honked the horned. All 3 off them left nasty stains on the board and made a multitude out of the the different products, and the enjoying cards and funds have been given slimed on. The cowboy on the pony and the motive force of the vehicle stored combating off at quite some inns searching for a bar. It replaced right into a nightmare. i quite concept it replaced right into a sturdy theory until i attempted it.

Still have questions? Get answers by asking now.