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.

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 this 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.