为什么即使定义了“标识符未定义错误”?

问题描述 投票:2回答:2

我创建了一个带有2个类的头文件ll.h。代码如下:

#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;
 }
 }

函数中的head似乎不应该调用任何错误。

我还是个初学者,请原谅我,如果它是微不足道的话。

我知道这不应该是一个问题,但我正在为类和声明本身使用不同的窗口。

我正在使用Visual Studio 2010运行代码。

c++ compiler-errors undeclared-identifier
2个回答
3
投票

1)这里:

N::~N{}

你忘记了析构者the parentheses~N()

N::~N(){};

2)这里:

void aFr(N* n){

和这里:

void aEn(N* n){

你忘了把use scope resolution operator表示为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;
}
}

这些更改后编译好。


0
投票

你忘了方法aFR和aEn的类方法声明(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;
 }
 }
© www.soinside.com 2019 - 2024. All rights reserved.