Hi,

I was working on Josephus problem, where I have to make a circular linked list and having errors in it. Will really appreciate if anyone can help me.

The following method is in Node Class:

void link(int num_people, Node* temp)
    {
        int n = num_people;

        [B]head->next = new Node;[/B]

        temp = head->next;
        for(int x = 2; x < n; x++)
        {
            temp->next = new Node;
            temp = temp->next;          
        }
    }

error C2065: 'head' : undeclared identifier (error in line which starts with head->next)
error C2227: left of '->next' must point to class/struct/union/generic type (error in line which starts with head->next)

error C2227: left of '->next' must point to class/struct/union/generic type(error in line which starts with temp = head->next)

Thanks in advance.

Dani AI

Generated

Three root issues appear in the thread: the list head is being declared in the wrong scope, pointer/member syntax is mixed up, and the member function was implemented as a free function (which produces the linker complaint). and diagnosed the pointer/operator and missing-definition problems; the durable fix is to make the list own the head pointer, keep Node minimal, and define the member function with the class scope operator in the .cpp file.

A compact, safe pattern to follow (not a verbatim repeat of earlier snippets) — Node is just a small struct, the list owns head, and set_number_people is defined with the proper scope:

struct Node {
    int id;
    Node* next;
    Node(int v = 0) : id(v), next(nullptr) {}
};

class CircularLinkedList {
    Node* head;
public:
    CircularLinkedList() : head(nullptr) {}
    ~CircularLinkedList();
    void set_number_people(int n);
};

void CircularLinkedList::set_number_people(int n) {
    if (n <= 0) return;
    head = new Node(1);
    Node* cur = head;
    for (int i = 2; i <= n; ++i) {
        cur->next = new Node(i);
        cur = cur->next;
    }
    cur->next = head; // close the circle
}

Key practical points: the linker error happens when the declaration (in the header) and the definition (in the .cpp) do not match — e.g., leaving off CircularLinkedList:: makes a different (free) function. Also ensure the .cpp is part of the build and do a full rebuild after changing signatures. Do not create head inside Node methods; that confuses ownership and leaks memory.

Finally, add a destructor to delete nodes (or use smart pointers/containers in modern C++), guard edge cases like n <= 1, and test with small values first. These changes resolve the symptoms discussed by and follow the hints from and .

Recommended Answers

All 10 Replies

I got the errors, I had mistakenly comment a line which was

Node *head = NULL;

But I am still have error in the following statements

void set_number_people(int num_people)
{
    Node *head = NULL;
    Node *temp = new Node;  
    temp.link(num_people, head);

}


error C2228: left of '.link' must have class/struct/union (error in the last line)

Sorry for the inconvenience, i got this error as well
Actually i was using java syntax that was I was using temp.link()
It should be temp->link().

Try this: temp->link(num_people, head); . You've declared the variable temp to be a pointer, but you are accessing its member function as though it were a normal object and not a pointer object. See for more info.

Thank you for your help.

Now I am having a different error.

error LNK2019: unresolved external symbol "public: void __thiscall CircularLinkedList::set_number_people(int)"

fatal error LNK1120: 1 unresolved externals

my driver.cpp is

#include<iostream>
#include "CircularLinkedList.h"

using namespace std;

void main()
{
    int people, count;
    cout<<"Enter number of people: ";
    cin>>people;
    cout<<"Enter number of counts: ";
    cin>>count;
    CircularLinkedList cclist;
    cclist.set_number_people(people);
}

Looking at the linker error, your function void CircularLinkedList::set_number_people(int); is declared, but not defined... :P May look something like this:

class CircularLinkedList {
      public:
            void set_number_people(int); // declaration
}; 
// And no definition

Add the definition(if it isn't there) and see.

I can't say more without looking at your header.

I do have the header and it is somewhat like this:

#ifndef CircularLinkedList_H
#define CircularLinkedList_H

class CircularLinkedList
{
public:
    void remove();
    void set_number_people(int);

};
#endif

*Sigh* I meant for you to check whether that particular function is both defined and declared... Just check whether the function set_number_people(int); is defined in the cpp file corresponding to your header.
Something like this (in your cpp file):

#include "CircularLinkedList.h"

void CircularLinkedList::set_number_people(int num) // this function definition
{                                                   // might be missing
    // do something here                            // *
}                                                   // *

It probably is missing, which would explain why the linker is complaining..

I am sorry that i have not given you the details but i do have the .cpp file as well which is:

#include "CircularLinkedList.h"
#include "Node.h"
#include <stdio.h>


void set_number_people(int num_people)
{
    Node *head = new Node;  
    head->link(num_people); 
}

and also the Node.h

#ifndef Node_H
#define Node_H
#include<stdio.h>

class Node
{
public:
    void set_data(int data)
    {
        this->data = data;
    }
    void link(int num_people)
    {
        int n = num_people;
        Node *head = NULL;
        head->next = new Node;      
        Node *temp = head->next;
        for(int x = 2; x < n; x++)
        {
            temp->next = new Node;
            temp = temp->next;          
        }
    }

private:
    int data;
    Node *next;
};
#endif

I hope this makes sense.

According to your above post the function set_number_people is not defined to be a part of any class but yet when you call it, you use an object to call the function

Thank you amrith92 and abhimanipal for your help.

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.