尝试编译程序时收到C2760错误。它来自这段代码。这是一个LinkedList.h文件,可与其他文件一起使用。
一切
void Insert(T data)
{
if (head == nullptr) //If the list is empty
{
head = new Node<T>(data, nullptr)
} //error C2760 Given from this line. Tutor was unable to fix
else
{
Node<T> *temp = head
Node<T> *tempT = nullptr
while (temp != nullptr && temp->data <data):
{
tempT = temp
temp = temp->next
}
if (temp == nullptr)
{
tempT->next = new Node<T>(data, nullptr)
}
else if (temp == head)
{
tempT = new Node<T>(data, head)
head = tempT
}
else
{
tempT->next = new Node<T>(data, temp)
}
}
}
状态 错误C2760语法错误:意外的标记'}',应为';'
此错误的解决方法
答案 0 :(得分:1)
您缺少语句中的分号。
即:
head = new Node<T>(data, nullptr);
您错过了几(9)个地方。另外,while循环上的冒号也是无效的。
因此,完整的更改将是:
void Insert(T data)
{
if (head == nullptr) //If the list is empty
{
head = new Node<T>(data, nullptr);
}
else
{
Node<T> *temp = head;
Node<T> *tempT = nullptr;
while (temp != nullptr && temp->data <data)
{
tempT = temp;
temp = temp->next;
}
if (temp == nullptr)
{
tempT->next = new Node<T>(data, nullptr);
}
else if (temp == head)
{
tempT = new Node<T>(data, head);
head = tempT;
}
else
{
tempT->next = new Node<T>(data, temp);
}
}