我正在尝试运行C ++代码,但我遇到了这个错误
Build started: Project: roman numerals, Configuration: Debug Win32 ------
1>Compiling...
1>Main.cpp
1>c:\users\owner\documents\dark gdk\projects\roman numerals\roman numerals\main.cpp(40) : error C2065: 'number' : undeclared identifier
1>c:\users\owner\documents\dark gdk\projects\roman numerals\roman numerals\main.cpp(40) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Users\Owner\Documents\Dark GDK\Projects\roman numerals\roman numerals\Debug\BuildLog.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);
}
答案 0 :(得分:3)
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
函数中,不能传播到它依次调用的函数。
答案 1 :(得分:3)
您可能希望将number
参数传递给display
函数。
int number = 0;
number = atoi(dbInput());
- display();
+ display(number);
}
-void display ()
+void display (int number)
{
while (number !=6)
{
答案 2 :(得分:0)
您在number
函数中使用display()
变量。这里无法进入。
答案 3 :(得分:0)
在Input()函数中声明变量'number',这意味着它只存在于此函数中。您正试图从另一个函数(第40行)使用它,编译器无法看到它。你需要做的是在代码顶部的任何函数之外放置数字(const int declerations是):
e.g
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
{
...
}
}