2

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.

Geezer
  • 5,600
  • 18
  • 31
Nithin K
  • 105
  • 3
  • 7
  • Note: You should put function definitions in source file (`.cpp`) and include your header with `#include "ll.h"` in that source file. – Yksisarvinen Sep 18 '18 at 08:01

2 Answers2

3

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.

Geezer
  • 5,600
  • 18
  • 31
  • Thanks this helped I forgot to add the scope resolutions for both the functions. Since they were in different.cpp files from the declarations i didn't notice them. – Nithin K Sep 18 '18 at 08:59
  • @NithinK Indeed friend. Glad to help. – Geezer Sep 18 '18 at 09:00
0

You forgot class method declaration (ll:) for methods aFR and aEn

 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;
 }
 }
PilouPili
  • 2,601
  • 2
  • 17
  • 31