C++ 在模板类中重载运算符时遇到问题
C++ Trouble overloading operators in a template 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;
}
}
正如你所说,这些不是编译错误:它们是智能感知错误。这些错误需要一段时间才能在扩展中刷新,因此大多数时候不是很指示,这是一个已知问题,智能感知在添加注释方面并不擅长,并且在与其他扩展发生冲突时更糟。
消除错误的一种方法是剪切粘贴所有代码(只需ctrl + a,ctrl + x,ctrl + v)。这会强制智能感知刷新。
我个人最喜欢的另一种方法是关闭智能感知:)您可以在此处了解如何执行此操作。
相关文章:
- 我在使用相等性测试布尔运算符时遇到问题
- 我在C++中遇到插入运算符错误
- 类模板的用户定义赋值运算符时遇到问题
- 错误 C2676;在C++的二叉搜索树类中定义 ++ 运算符时遇到问题
- 运算符重载<<,打印对象矢量时遇到问题
- 为什么在C 中调用模板超载运算符时,我会遇到细分故障
- 编译器在遇到提取或插入运算符时处理信息(字符串、操纵器等)的顺序是什么?
- 重载类<<运算符和类的标头时遇到问题
- 我在C++中使用删除运算符时遇到问题。我在运行时不断收到"double free or corruption (fasttop)"错误
- 使用引用和取消引用运算符读取代码行时遇到问题
- C++ 在模板类中重载运算符时遇到问题
- 我在实现++增量运算符时遇到问题
- 在 c++ 运算符中使用友元函数访问私有成员时遇到问题<<过载
- 使用前增量和后增量运算符将 c++ 转换为 python 时遇到问题
- C++运算符重载时遇到问题
- 请在使用新运算符时遇到问题
- 在 c++ 头文件中声明重载运算符时遇到问题
- 我在运算符函数的运算符重载时遇到问题
- 运算符重载 - 知道为什么我在 c++ 程序中遇到这两个错误吗?
- 在bigint类上对+和*进行运算符重载时,我会遇到丢弃限定符的错误