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

C++ 使用数据库结构的简单程序中的未声明标识符错误,c++,compiler-errors,C++,Compiler Errors,我的编译器没有编译我编写的以下程序。它在我标记的行中给出了一个错误,指出标识符没有声明,即使我在主函数中声明了它 程序是不完整的,但它将获取有关活动的输入并输出 #include <iostream.h> #include <conio.h> void addToLog(); void viewLog(); void what() { cout << "What would you like to do?" << endl

我的编译器没有编译我编写的以下程序。它在我标记的行中给出了一个错误,指出标识符没有声明,即使我在主函数中声明了它

程序是不完整的,但它将获取有关活动的输入并输出

#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();
}
您已经在main中将db声明为局部变量;其他函数无法看到它

至少有两种解决方案:

使db成为全局静态变量-即将其声明/定义移出main。通常不建议这样做,因为过度依赖全局变量通常是不好的做法。 将指向db的指针传递给需要它的函数。
@chris dude这是一个学校项目,我被迫不使用返回类型。a它应该被标记为家庭作业,b你应该问你的老师为什么你对main有如此荒谬的限制。@chris-我记得这个标记。顺便说一下,我的老师推荐返回类型,但是她说这对我的班里的其他人是不公平的,因为她希望我们使用C++的所有概念,而不仅仅是先进的东西。这是你们老师的一个有趣的方法,但是C++标准说main必须返回int。VC++ main甚至不为My编译。初学者或高级。除了使用指针,没有其他方法可以做到这一点吗?我必须为一个项目使用这段代码,我不能使用指针等,我不知道如何使它全球化。我只是把我的结构移到main之外吗?@RohanVerma:是的,把数据库和数据库的定义移到main之外。