"Use of undeclared identifier 'IntQueue' "即使它被定义

"Use of undeclared identifier 'IntQueue' " even though it is defined

本文关键字:定义 IntQueue undeclared of Use identifier      更新时间:2023-10-16

我收到一条错误消息,说在vscode中使用了未声明的标识符"IntQueue",我无法弄清楚出了什么问题。

我尝试重命名文件,但仍然不起作用。我创建了一个单独的头文件,该文件定义了类,并将头文件包含在定义了所有构造函数的主 cpp 文件中。但是我想不出解决问题的方法。

//This is the IntQueue.h header file
#ifdef _IntQueue_
#define _IntQueue_
#include<iostream>
#include<fstream>
using namespace std;
class IntQueue {
    int* numbers;
    int size;
    int front;
    int back;
public:
    IntQueue (unsigned int n);
    IntQueue();
    ~IntQueue();
    int getSize() {return size;}
    int getFront() {return front;}
    int getBack() {return back;}
    void incSize();
    void pop();
    int frontNumber();
    void push(int i);
    void reverse();
};
#endif
//This is the IntQueue.cpp file (incomplete)
#include "IntQueue.h"
IntQueue::IntQueue (unsigned int n) {
    size = n;
    numbers = new int[size];
    front = 0;
    back = 0;
}
IntQueue::IntQueue() {
    size = 100;
    front = 0;
    back = 0
    numbers = new int [size];
}

你需要改变

#ifdef _IntQueue_

#ifndef _IntQueue_

当您的.cpp文件#include您的.h文件时,_IntQueue_尚未声明,因此#ifdef会跳过.h文件的全部内容,因此编译器对IntQueue类一无所知。