视觉错误在使用 #include "DarkGDK.h" 编译程序时出现C++

visual Error coming while compiling C++ program using #include "DarkGDK.h"

本文关键字:编译程序 C++ DarkGDK #include 视觉 错误      更新时间:2023-10-16

我正在尝试运行c++代码,但我面临这个错误

Build started: Project: roman numerals, Configuration: Debug Win32 ------
1>Compiling...
1>Main.cpp
1>c:usersownerdocumentsdark gdkprojectsroman numeralsroman numeralsmain.cpp(40) : error C2065: 'number' : undeclared identifier
1>c:usersownerdocumentsdark gdkprojectsroman numeralsroman numeralsmain.cpp(40) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:UsersOwnerDocumentsDark GDKProjectsroman numeralsroman numeralsDebugBuildLog.htm"
1>roman numerals - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是代码:

#include "DarkGDK.h"
const int I = 1;
const int II = 2;
const int III = 3;
const int IV = 4;
const int V = 5;
void setup();
void input();
void display();
void cleanup();
void DarkGDK()
{
      setup();
      input();
      cleanup();
}
void setup()
{
       dbSetWindowOff();
       dbLoadBitmap("C:\Users\Owners\Pictures\DeVry\roman numeral 1.bmp",I);
       dbLoadBitmap("C:\Users\Owners\Pictures\DeVry\roman numeral 2.bmp",II);
       dbLoadBitmap("C:\Users\Owners\Pictures\DeVry\roman numeral 3.bmp",III);
       dbLoadBitmap("C:\Users\Owners\Pictures\DeVry\roamn numeral 4.bmp",IV);
       dbLoadBitmap("C:\Users\Owners\Pictures\DeVry\roamn numeral 5.bmp",V);
}
void input()
{
       dbPrint("enter a number1 to 5 to see its roman numeral");
       int number = 0;
       number = atoi(dbInput());
       display();
}
void display ()
{
       while (number !=6)
       {
              switch (number)
              {
              case 1:
                   dbCopyBitmap(I,0);
                   break;
              case 2:
                   dbCopyBitmap(II,0);
                   break;
              case 3:
                   dbCopyBitmap(III,0);
                  break;
             case 4:
                  dbCopyBitmap(IV,0);
                 break;
             case 5:
                  dbCopyBitmap(V,0);
                  break;
             case 6:
                  dbPrint("ending program");
                  dbWait(2000);
             default:
                  dbPrint("not a valid choice, only numbers 1 through 5 work");
                 dbPrint("try again");
              }
       }
}
void cleanup ()
{
       dbDeleteImage(I);
       dbDeleteImage(II);
       dbDeleteImage(III);
       dbDeleteImage(IV);
       dbDeleteImage(V);
}
void input()
{
       dbPrint("enter a number1 to 5 to see its roman numeral");
       int number = 0;
       number = atoi(dbInput());
       display();
}
void display ()
{
       while (number !=6)
       {
        // ...

display函数在其作用域中没有名为number的变量可访问。number的作用域仅在input函数中,不能传播到它依次调用的函数

您可能需要将number参数传递给display函数。

        int number = 0;
        number = atoi(dbInput());
-       display();
+       display(number);
 }
-void display ()
+void display (int number)
 {
        while (number !=6)
        {

您在display()函数中使用number变量。

你在Input()函数中声明了变量'number',这意味着它只存在于这个函数中。您试图从另一个函数(在第40行)使用它,编译器看不到它。您需要做的是将number放在代码顶部的任何函数之外(const int声明所在的位置):

int number;
void input()
{
       dbPrint("enter a number1 to 5 to see its roman numeral");
       number = atoi(dbInput()); //Set the value of number here
       display();
}
void display ()
{
       while (number !=6) //use number here
       {
           ...
       }
}