Search code examples
c++compiler-errorsundeclared-identifier

Why is there a "Identifier is undefined error " even after it's been defined?


I've created a header file ll.h with 2 classes in it. The code goes something like this:

#pragma once
#include<iostream>
#include<conio.h>
using namespace std;

class N{
public:
   int data;
    N *next;
 public:
     N(int);
     ~N();
  };

  class ll{
  public:
      N *head;
  public:
      ll();
      ~ll();
      void aFr(N*);
      void aEn(N*);
   };

  N::N (int d){
  data = d;
  next = NULL;
  }

  N::~N{}

  ll::ll(){
  head = NULL;
  }

  ll::~ll(){}

  void aFr(N* n){
  n->next = head; //identifier "head" undefined
  head = n;
  }

 void aEn(N* n){
 if (head == NULL)// identifier "head" undefined
 {
 head = n;
 n->next = NULL;
 }
 }

The head in both the function seems like it should not invoke any errors though.

I'm still a beginner so forgive me if it's something trivial.

I know it shouldn't be a problem but I was using different windows for both the classes and the declaration themselves.

I'm using Visual Studio 2010 run the code.


Solution

  • 1) Here:

    N::~N{}
    

    you forgot the parentheses of the destructor ~N():

    N::~N(){};
    

    2) Here:

    void aFr(N* n){
    

    and here:

    void aEn(N* n){
    

    You forgot to use scope resolution operator to denote the functions as methods of class ll

    void ll::aFr(N* n){
    n->next = head; //identifier "head" undefined
    head = n;
    }
    
    void ll::aEn(N* n){
    if (head == NULL)// identifier "head" undefined
    {
    head = n;
    n->next = NULL;
    }
    }
    

    This compiles fine after these changes.