c++ - 使用数据库结构的简单程序中未声明的标识符错误

标签 c++ compiler-errors

这个问题不太可能帮助任何 future 的访客;它只与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the help center .




9年前关闭。




我的编译器不会编译我编写的以下程序。它在我标记的行中给出了一个错误,指出“未声明标识符”,即使我已经在我的 Main() 函数中声明了它。

该程序是不完整的,但它将接受有关事件的输入并输出它。

#include <iostream.h>
#include <conio.h>


void addToLog();
void viewLog();

void what()
{
    cout << "What would you like to do?" << endl
         << "1. View Today's Log" << endl
         << "2. Add to Today's Log" << endl
         << "__________________________" << endl << endl
         << "? -> ";

    int in;

    cin  >> in;

    if ( in == 1 )
    {
        viewLog();
    }

    if ( in == 2 )
    {
        addToLog();
    }

}

void main()
{
    clrscr();


    struct database
    {
        char act[20];
        int time;
    };

    database db[24];



    what();



    getch();
}

void addToLog()
{
    int i=0;
    while (db[i].time == 0) i++;

    cout    << endl
        << "_______________________________"
        << "Enter Activity Name: ";
    cin     >> db[i].act;                           // <-------------
    cout    << "Enter Amount of time: ";
    cin >> db[i].time;
    cout    << "_______________________________";

    what();

}

void viewLog()
{
    int i=0;
    cout    << "_______________________________";
    for (i = 0; i <= 24; i++)
    {
        cout    << "1. " << db[i].act << "   " << db[i].time << endl; // <-------
    }
    cout    << "_______________________________";

    what();
}

最佳答案

您已声明 db作为 main() 中的局部变量;它不能被其他功能看到。

至少有两种解决方案:

  • 制作 db全局(静态)变量 - 即将其声明/定义移出 main() . 通常不建议这样做,因为过度依赖全局变量通常是不好的做法。
  • 传递指向 db 的指针进入需要它的功能。
  • 关于c++ - 使用数据库结构的简单程序中未声明的标识符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10372465/

    相关文章:

    c++ - while 循环的问题

    c++ - 来自基类的函数指针

    java - 为什么我无法定义数组?

    scala - Play Framework 类型不匹配;找到了: Int required: String

    c++ - 在bigint类的+和*上进行运算符重载时,在丢弃限定符时出现错误

    c++ - 基本 C++ 原子数组

    c++ - 在C++中捕获shell脚本退出状态

    c++ - 我需要类似 "using Base::*;"的东西

    macos - 尝试执行 cc1 时出错,gcc 只想为 avr 编译?

    vb.net - 修复 'Error class is ambiguous in the namespace' 保持其模糊性