C++ 无法在模板中重载运算符 class
C++ Trouble overloading operators in a template class
每次我在运算符的定义中添加注释时,它开始给我错误,但是删除注释会立即消除错误。我不明白为什么注释会对代码产生任何影响。另外,对于一般运算符重载的一般性建议,我们将不胜感激。
这是我的 class 模板:
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;
}
}
如您所说,这些不是编译错误:它们是 Intellisense 错误。这些错误需要一段时间才能在扩展中刷新,因此大多数时候都不是很明显,而且这是一个已知问题,即 Intellisense 在添加注释时效果不佳,在与其他扩展冲突时更糟。
消除错误的一种方法是剪切粘贴所有代码(只需按 ctrl+a、ctrl+x、ctrl+v)。这会强制 Intellisense 刷新。
我个人最喜欢的另一种方法是关闭 Intellisense :) 你可以看到如何做到这一点 in here。
每次我在运算符的定义中添加注释时,它开始给我错误,但是删除注释会立即消除错误。我不明白为什么注释会对代码产生任何影响。另外,对于一般运算符重载的一般性建议,我们将不胜感激。
这是我的 class 模板:
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;
}
}
如您所说,这些不是编译错误:它们是 Intellisense 错误。这些错误需要一段时间才能在扩展中刷新,因此大多数时候都不是很明显,而且这是一个已知问题,即 Intellisense 在添加注释时效果不佳,在与其他扩展冲突时更糟。
消除错误的一种方法是剪切粘贴所有代码(只需按 ctrl+a、ctrl+x、ctrl+v)。这会强制 Intellisense 刷新。
我个人最喜欢的另一种方法是关闭 Intellisense :) 你可以看到如何做到这一点 in here。