C ++在模板类中重载运算符的麻烦

时间:2015-03-09 04:38:19

标签: c++ templates operator-overloading

每次我在运算符的定义中添加注释时,它都会开始给我错误,但删除注释会立即消除错误。我不明白为什么评论会对代码产生任何影响。另外,对于一般操作员超载的一般建议也不胜感激。

继承我的班级模板:

template<class THING>
struct LLNode
{
  THING data;
  LLNode<THING> *next;
  LLNode<THING> *prev;
};
template<class THING>
class LinkedList
{
private:
     //use a doubly linked-list based implementation
     //keep a head and tail pointer for efficiency
     LLNode<THING> *Head;
     LLNode<THING> *Tail;
     int count;
public:
     //setup initial conditions
     LinkedList();
     //delete all dynamic memory, etc.
     ~LinkedList();
     //constant bracket operator to access specific element
     const THING& operator[](int);
     //Bracket operator to access specific element
     THING& operator[](int);
     //Equality operator to check if two lists are equal
     bool operator==(const LinkedList<THING>&);
     //Inequality operator to check if two lists are equal
     bool operator!=(const LinkedList<THING>&);
     //add x to front of list
     void addFront(THING);
     //add x to back of list
     void addBack(THING);
     //add x as the ith thing in the list
     //if there are less than i things, add it to the back
     void add(THING, int);
     //remove and return front item from list
     THING removeFront();
     //remove and return back item from list
     THING removeBack();
     //return value of back item (but don't remove it)
     THING getBack();
     //return value of front item (but don't remove it)
     THING getFront();
     //return how many items are in the list
     int length();
     //print all elements in the linked list
     void print();
};

我正在处理的运营商:

template<class THING>
THING& LinkedList<THING>::operator[](int index)
{

}

template<class THING>
bool LinkedList<THING>::operator==(const LinkedList<THING>& list_one, const LinkedList<THING>& list_two)
{
    //checking for same size on both lists
    //if both are same size, move on to checking for same data
    if(list_one.count != list_two.count)
    {
        return false;
    }
    else
    {
        //boolean flag to hold truth of sameness
        bool flag = true;
        //two pointers to go through
        LLNode<THING> *temp_one = list_one.Head;
        LLNode<THING> *temp_two = list_two.Head;
        while(temp_one != NULL && temp_two != NULL)
        {
            if(temp_one->data != temp_two->data)
            {
                flag = false;
                break;
            }
            else
            {
                temp_one = temp_one->next;
                temp_two = temp_two->next;
            }
        }
        return flag;
    }
}

1 个答案:

答案 0 :(得分:0)

正如您所说,这些不是编译错误:它们是智能感知错误。这些错误需要一段时间才能在扩展中刷新,因此在大多数情况下并不是非常有说明性的,并且Intellisense在添加注释时并不是一个很好的问题,并且在碰撞时更加糟糕与其他扩展。

摆脱错误的一种方法是剪切粘贴所有代码(只需去 ctrl + a,ctrl + x,ctrl + v )。这迫使Intellisense刷新。

我个人最喜欢的另一种方式是关闭Intellisense :)你可以看到如何做到这一点in here