Hello. I am working on a data structures program that should be quite easy, but after programming in assembly language using mips, I forgot a little bit of C++ and can't remember how to do this. I've used my googlefu for at least 2 hours and still have this problem.
The error:
head undeclared, first use this function.

The code it is talking about

void node::insertNode(int id, string name, double average,int *array)
{ 
     node *newNode; //a new node
     node *nodePtr; // to traverse the list
     node *previousNode = NULL; //the previous node
     cout << "test 2";
     //allocate a new node and store the values there
     newNode = new node;
     newNode->data.setStudentAverage(average);
     newNode->data.setStudentName(name);
     newNode->data.setStudentId(id);
     newNode->data.setStudentTests(array);
     cout << "test 3";
     //If there are no nodes in the list
     //make newNode the first node
     if(!head)
     {
              cout << "test 4";
              head = newNode;
              newNode->next = NULL;
              
              }

There is more but the program never made it to test 4.
Now it won't even complile.
Here is the node part of the header file.

class node
{
private:
        student data;
        node *next;
public:
       node();
       void insertNode(int id, string name, double average, int *array);
       void deleteNode(int id);
       void displayList();
       
      };

#endif

And here is the constructor within the cpp

node::node()
{
            node *head;
            }

I don't know exactly where I'm not declaring it seeing as I am pretty sure that is the declaration. please help if you can and I'm sorry if it's completely messy.

Thank you for your help! I am still trying to figure it out on my own.

Recommended Answers

All 3 Replies

It's a problem of context.
The pointer node* head is local to the constructor.

Depending on what it is you are trying to do, maybe a static node *head member will be the right choice for you?

It's a problem of context.
The pointer node* head is local to the constructor.

Depending on what it is you are trying to do, maybe a static node *head member will be the right choice for you?

That didn't seem to work either.
What I'm trying to do is have a node of a class Student.
Every student has an ID, Average, Array of Tests, and a Name. The node is supposed to hold a student.
I thought a constructor would make it where head is initially declared, and the functions would take hold of the rest.
I tried the static node *head, in the constructor but the same error came up

Okay I figured out the problem. In the node, under public, all I did was add node *head;

in the constructor, I did head = NULL;

that solved it, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.