You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

编译器错误信息各部分含义解析:以customer_count未定义错误为例

解析C++编译错误:‘customer_count’ was not declared in this scope

先把你遇到的错误信息贴出来:

main.cpp:22:21: error: ‘customer_count’ was not declared in this scope

我来逐个拆解这个错误信息里每个部分的含义:

  • main.cpp:这是你正在编译的源代码文件名,编译器明确告诉你错误出在这个文件里。
  • 22:错误所在的行号,对应你代码里的for(int i = 0; i < customer_count; i++)这一行,也就是第22行。
  • 21:错误在该行的列位置——从行首开始数第21个字符的位置,刚好是customer_count这个变量的起始处,编译器就是在这里发现了它不认识的标识符。
  • error:这是一个编译错误(不是警告),意味着不修复这个问题,编译器根本没法生成可执行程序。
  • ‘customer_count’ was not declared in this scope:这是核心问题——你在get_input_from_user函数里用了customer_count这个变量,但它既没有在函数内部声明,也没有在全局范围内定义过,编译器完全不知道这个变量是什么,所以直接报错。

顺便给你个简单的修复思路:把customer_count作为参数传给get_input_from_user函数,比如修改函数定义:

void get_input_from_user(Customer * ptr, int customer_count){
    cin.ignore();
    for(int i = 0; i < customer_count; i++){
        cout<<"Enter Customer Name (1-20 character long)"<<endl;
        cin.getline(ptr[i].name,20);
        cout<<"Enter Cell No. (11 character long)"<<endl;
        cin.getline(ptr[i].cell,20);
        cout<<"Enter Initial Balance"<<endl;
        cin >> ptr[i].bal;
        cin.ignore();
    }
}

这样调用这个函数的时候,传入你需要处理的客户数量就行。

内容的提问来源于stack exchange,提问作者M.M.

火山引擎 最新活动