سؤال

أحاول استخدام نوع بيانات مخصص في قائمة مرتبطة بشكل مضاعف.أنا قادر على إنشاء القائمة، ولكن عندما أحاول استدعاء وظيفة الإدراج، يظهر خطأ.ما الذي يسبب هذا الخطأ؟

main.cpp:

#include "command.h" //my custom class 
#include "doublyLinkedList.h"
int main(){

    //create a queue of jobs:
    doublyLinkedList<command>* queue = new doublyLinkedList<command>;

    //creating a new command:
    command *c = new command();
    c->createCommand();
    c->print();
    const command *d = new command(c->name, c->description, c->shellString);

    queue->insert(*d); //problem line
};

وظيفة الإدراج في doublyLinkedList.h:

template <class Type>
void doublyLinkedList<Type>::insert(const Type& insertItem)
{
nodeType<Type> *current;      //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode;      //pointer to create a node
bool found;

newNode = new nodeType<Type>; //create the node
newNode->info = insertItem;  //store the new item in the node
newNode->next = NULL;
newNode->back = NULL;

if(first == NULL) //if the list is empty, newNode is 
                  //the only node
{
   first = newNode;
   last = newNode;
   count++;
}
else
{
    found = false;
    current = first;

    while (current != NULL && !found) //search the list
        if (current->info >= insertItem)
            found = true;
        else
        {
            trailCurrent = current;
            current = current->next;
        }

    if (current == first) //insert newNode before first
    {
        first->back = newNode;
        newNode->next = first;
        first = newNode;
        count++;
    }
    else
    {
          //insert newNode between trailCurrent and current
        if (current != NULL)
        {
            trailCurrent->next = newNode;
            newNode->back = trailCurrent;
            newNode->next = current;
            current->back = newNode;
        }
        else
        {
            trailCurrent->next = newNode;
            newNode->back = trailCurrent;
            last = newNode;
        }

        count++;
    }//end else
}//end else
}//end insert

تعمل التعليمات البرمجية مع ints والسلاسل، ولكن ليس مع أنواع "الأوامر".

خطأ:

In file included from main.cpp:2:0:
doublyLinkedList.h: In member function âvoid doublyLinkedList<Type>::insert(const Type&) [with Type = command]â:
main.cpp:34:18:   instantiated from here
doublyLinkedList.h:171:13: error: no match for âoperator>=â in âcurrent->nodeType<command>::info >= insertItemâ
doublyLinkedList.h:171:13: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:232:5: note: template<class _T1, class _T2> bool std::operator>=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:315:5: note: template<class _Iterator> bool std::operator>=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:365:5: note: template<class _IteratorL, class _IteratorR> bool std::operator>=(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/basic_string.h:2621:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2633:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2645:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
هل كانت مفيدة؟

المحلول

يبدو أنك تحاول إبقاء القائمة مرتبة، ولكنك لم تحدد عوامل المقارنة لها command.تحتاج إلى التنفيذ operator>= لنوعك، إذا كنت تريد استخدامه كما هو.

من المحتمل أن يبدو توقيعه كما يلي:

bool operator>= (const command& a, const command& b);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top