-2

I receive a C2760 error when trying to compile my program. It is coming from this section of code. This is a LinkedList.h file that works with other files.

everything

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)
        }
    }
}

State Error C2760 syntax error: unexpected token '}', expected ';'

a fix in this error

  • 4
    Consider learning C++ from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). All of the statements in your code are missing `;` at the end of them. – Algirdas Preidžius Apr 19 '19 at 21:22
  • Always post the full error message. In Visual Studio you can easily copy this from the Output tab. And yes I mean Output Tab and not the Errors List. The Errors list contains a reduced version of the error message and is more difficult to copy because its split in several columns. – drescherjm Apr 19 '19 at 22:12

1 Answers1

1

You are missing the semicolons on the statements.

ie:

head = new Node<T>(data, nullptr);

You have missed in several (9) places. In addition the colon on the while loop is also invalid.

So full changes would be:

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);
    }
}
lostbard
  • 5,065
  • 1
  • 15
  • 17
  • After fixing that, I receive the same error Severity Code Description Project File Line Suppression State Error C2760 syntax error: unexpected token 'identifier', expected 'declaration' for the line "Node *tempT = nullptr " – Job Plumpp Apr 19 '19 at 21:26
  • @JobPlumpp Quoting the answer "_You have missed in several places_", which means - you need to add the missing semicolon on all of the lines that's missing it, in a similar fashion. – Algirdas Preidžius Apr 19 '19 at 21:29
  • I have added all of the semicolons but still receive error C2760 relating to the else if statements. Thank you for the help. – Job Plumpp Apr 19 '19 at 21:39
  • are you sure you have ALL the semicolons? Also without a minimal full code, its hard to know whether you missed something else prior – lostbard Apr 19 '19 at 22:16