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

胡安·莫雷诺(Juan Moreno)

每次我在运算符的定义内添加注释时,都会开始给我带来错误,但是删除注释会立即消除错误。我不明白为什么注释对代码完全没有影响。同样,对于一般的操作员过载而言,一般的建议也将受到赞赏。

这是我的班级模板:

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;
    }
}
谢伊·内马德(Shay Nehmad)

正如您所说的,这些不是编译错误:它们是Intellisense错误。这些错误需要一段时间才能在扩展中刷新,因此在大多数情况下并不是很明显,这是一个已知的问题,Intellisense不能添加注释,与其他扩展冲突时甚至更糟。

消除错误的一种方法是剪切所有代码(只需ctrl + a,ctrl + x,ctrl + v)。这将迫使Intellisense刷新。

我个人最喜欢的另一种方法是关闭Intellisense :)您可以在此处查看如何执行此操作

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章