【问题标题】:Why is there a "Identifier is undefined error " even after it's been defined?为什么即使定义了“标识符未定义错误”?
【发布时间】:2018-09-18 07:51:10
【问题描述】:

我创建了一个头文件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 运行代码。

【问题讨论】:

  • 注意:您应该将函数定义放在源文件 (.cpp) 中,并在该源文件中包含带有 #include "ll.h" 的标头。

标签: c++ compiler-errors undeclared-identifier


【解决方案1】:

1) 这里:

N::~N{}

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

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

这些更改后编译正常。

【讨论】:

  • 感谢这帮助我忘记了为这两个函数添加范围分辨率。由于它们与声明位于不同的.cpp 文件中,因此我没有注意到它们。
  • @NithinK 确实是朋友。很高兴能提供帮助。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-05
    • 2015-05-12
    • 2012-02-26
    • 2011-10-23
    • 1970-01-01
    • 2013-04-01
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多