You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用数组实现队列时,类名小写queue报错大写Queue正常的原因

为什么自定义类名queue会报错,改用Queue就正常?

先看你提供的代码:

// Implementing queue using array:
#include<bits/stdc++.h>
using namespace std;
class queue{
    public:
    int size;
    int f;
    int r;
    int *arr;
};
int isFull(queue *q)
{
    if(q->r==q->size-1)
    {
        return 1;
    }
    else{
        return 0;
    }
}
int enqueue(queue* q, int val){
    if(isFull(q))
    {
        cout<<"Overflow Condition";
    }
    else{
//      q->
cout<<"hi";
    }
}
int main()
{
    queue *q = new queue();
    q->f=q->r=-1;
    q->size=10;
    q->arr= new int[q->size];
    
    
}

原因解释

  • 问题核心是命名冲突:你写了using namespace std;,这条语句会把C++标准库std命名空间下的所有标识符直接导入到当前全局命名空间。
  • C++标准库本身就内置了一个容器类std::queue,当你自定义class queue时,编译器无法区分你要使用的是自己的类,还是标准库的queue容器,因此触发编译报错。
  • 改用首字母大写的Queue后,这个名称和标准库的queue没有重复,命名冲突消失,编译器能正常识别你的自定义类,程序自然可以正常运行。

额外建议

  • 尽量避免直接用using namespace std;,改用std::前缀访问标准库内容(比如std::cout),能从根源上避免这类命名冲突。
  • 也可以给自定义类加专属命名空间,比如:
    namespace MyOwnContainers {
        class queue {
            // 类定义内容
        };
    }
    
    使用时通过MyOwnContainers::queue访问,也能彻底规避冲突。

内容的提问来源于stack exchange,提问作者Rishi Matura

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 18:41:04