0

For some reason, I keep getting an "identifier not found" error whenever I try to use appendNode(). In addition, I keep getting an error that tells me I'm missing ";" before "using" which doesn't make any sense.

header file

#ifndef INTEGERLIST_H
#define INTEGERLIST_H
#include <iostream>
using namespace std;

class IntegerList
{
private:
    struct ListNode
    {
        int value;
        struct ListNode *next;
    };

    ListNode *head;

public:
    IntegerList()
    {
        head = NULL;
    }

    ~IntegerList();

    void appendNode(int);
    void insertNode(int);
    void deleteNode(int);
    void printList() const;
}
#endif

Implementation file

#include "IntegerList.h"
#include <iostream>
using namespace std;


void IntegerList::appendNode(int num)
{
    ListNode *newNode;
    ListNode *nodePtr;

    //new allocation
    newNode = new ListNode;
    newNode->value = num;
    newNode->next = NULL;

    if (!head)
        head = newNode;

    else
    {
        nodePtr = head;

        while(nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }
}

void IntegerList::insertNode(int num)
{
    ListNode *newNode;
    ListNode *nodePtr;
    ListNode *previousNode = NULL;

    newNode = new ListNode;
    newNode->value = num;

    if(!head)
    {
        head = newNode;
        newNode->next = NULL;
    }
    else
    {
        nodePtr = head;

        previousNode = NULL;

        while(nodePtr != NULL && nodePtr->value < num)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        if(previousNode == NULL)
        {
            head = newNode;
            newNode->next = nodePtr;
        }
        else
        {
            previousNode->next = newNode;
            newNode->next = nodePtr;
        }
    }
}

void IntegerList::deleteNode(int num)
{
    ListNode *nodePtr;
    ListNode *previousNode;

    if(!head)
        return;

    if(head->value == num)
    {
        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }

    else
    {
        nodePtr = head;

        while(nodePtr != NULL && nodePtr->value != num)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        if(nodePtr)
        {
            previousNode->next = nodePtr->next;
            delete nodePtr;
        }
    }
}

void IntegerList::printList() const
{
    ListNode *nodePtr;

    nodePtr = head;

    while(nodePtr)
    {
        cout << nodePtr->value << endl;

        nodePtr = nodePtr->next;
    }
}
mah
  • 39,056
  • 9
  • 76
  • 93
  • 7
    **Never** put a using directive in a header. No one wants to include a header and get `std` all of a sudden. – chris Apr 11 '14 at 19:35
  • 1
    You're missing a semicolon on the end of your class. – Will Custode Apr 11 '14 at 19:36
  • 2
    Perhaps you have the infamous bug of the missing `;` after your class declaration. I remember being warned about that in college. – Gareth Apr 11 '14 at 19:36

3 Answers3

1

#include does a straight copy from the other file, and since this is done by the preprocessor, the compiler doesn't really know this happened and gives you the error as if it was just one large file. So in your code you have (simplified below):

// header.h
class A{
   // ...
}

and

// code.cpp
#include "header.h"
using namespace std;

So this is converted to:

// code.cpp
class A{
   // ...
}
using namespace std;

This makes the error clearer. It is expecting a ; before using since class declarations must end in a ;.

clcto
  • 9,530
  • 20
  • 42
0

As mentioned in the comments, you're missing the ; at the end of your class declaration. You are getting the error in your implementation file because the header is included (copied in its entirety) right before your using statement. Makes perfect sense once you know what you're looking at.

Gareth
  • 3,502
  • 4
  • 20
  • 21
0

As others have already said, you're missing a ; from the end of your class declaration in your header.

However, the far more serious problem with the code is the line using namespace std; in the header. This is bad practice everywhere but particularly in header files. The only line that even uses anything from std is the single line in printList

    cout << nodePtr->value << endl;

Remove both of the using namespace std lines and replace the line in printList with this:

    std::cout << nodePtr->value << std::endl;

You and everyone else who might later use your code will thank you for it.

Community
  • 1
  • 1
Edward
  • 6,964
  • 2
  • 29
  • 55