为什么即使在定义之后仍存在"Identifier is undefined error "?

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

本文关键字:Identifier is undefined error 存在 定义 之后 为什么      更新时间:2023-10-16

我创建了一个头文件ll.h其中包含 2 个类。代码是这样的:

#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运行代码。

1(在这里:

N::~N{}

您忘记了析构函数~N()括号

N::~N(){};

2(在这里:

void aFr(N* n){

在这里:

void aEn(N* n){

您忘记使用范围解析运算符将函数表示为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;
}
}

这些更改后,这编译得很好。

您忘记了方法 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;
}
}