提问人:Ishan Srivastava 提问时间:3/25/2023 最后编辑:Nicol BolasIshan Srivastava 更新时间:3/25/2023 访问量:44
Struct 在全局空间中不适用于一个程序,但适用于另一个程序
Struct not working in Global space for one program but, works for another
问:
我对C++比较陌生。我最近在书中学习了C++的结构,它有以下代码来添加以英尺和英寸为单位的测量值。下面粘贴的代码按预期工作:
#include <iostream>
using namespace std;
struct Distance {
int feet;
float inches;
};
int main(){
Distance d1, d3;
Distance d2 = { 11, 6.25 };
cout << "\nEnter feet: "; cin >> d1.feet;
cout << "Enter inches: "; cin >> d1.inches;
d3.inches = d1.inches + d2.inches;
d3.feet = 0;
if(d3.inches >= 12.0)
{
d3.inches -= 12.0;
d3.feet++;
}
d3.feet += d1.feet + d2.feet;
cout << d1.feet << "\'-" << d1.inches << "\" + ";
cout << d2.feet << "\'-" << d2.inches << "\" = ";
cout << d3.feet << "\'-" << d3.inches << "\"\n";
return 0;
}
正如你所看到的,结构变量是在主函数内部定义的,而结构是在主函数外部定义的。
我还决定测试我的知识并制作一个类似的程序,这将增加时间,如下所示:
#include <iostream>
using namespace std;
// This function basically calculates how much is carried over to the next quantity for eg. if seconds //is 122, it will return 2 which will be carried to minutes.
int carry (int inp) {
int carry;
carry = inp / 60;
return carry;
}
struct time {
int hours;
int minutes;
int seconds;
};
int main(){
char ques = 'y';
time time1, time2, timetot;
do {
cout << "Want to add time (y/n) ? : "; cin >> ques;
if (ques != 'y') break;
cout << "Hours : " ; cin >> time1.hours ; cout << endl;
cout << "Minutes : " ; cin >> time1.minutes; cout << endl;
cout << "Seconds : " ; cin >> time1.seconds; cout << endl;
cout << "Hours : " ; cin >> time2.hours ; cout << endl;
cout << "Minutes : " ; cin >> time2.minutes; cout << endl;
cout << "Seconds : " ; cin >> time2.seconds; cout << endl;
if (time1.seconds >=60) { time1.minutes += carry(time1.seconds); time1.seconds %= 60; }
if (time1.minutes >=60) { time1.hours += carry(time1.minutes); time1.minutes %= 60; }
if (time2.seconds >=60) { time2.minutes += carry(time2.seconds); time2.seconds %= 60; }
if (time2.minutes >=60) { time2.hours += carry(time2.minutes); time2.minutes %= 60; }
timetot.hours = time1.hours + time2.hours ;
timetot.minutes = time1.minutes + time2.minutes ;
timetot.seconds = time1.seconds + time2.seconds ;
if (time1.seconds >=60) { time1.minutes += carry(time1.seconds); }
if (time1.minutes >=60) { time1.hours += carry(time1.minutes); }
if (time2.seconds >=60) { time2.minutes += carry(time2.seconds); }
if (time2.minutes >=60) { time2.hours += carry(time2.minutes); }
cout << "Tot Hours" << timetot.hours << endl;
cout << "Tot minutes" << timetot.minutes << endl;
cout << "Tot seconds" << timetot.seconds << endl;
}
while (ques=='y') ;
return 0;
};
上面的代码生成错误,即 time1、time2、time3 未定义,但是,我已经明确定义了这些结构变量。我也知道上面的代码没有语法错误,因为当我尝试在 main() 函数中定义结构时,程序按预期工作。我也在下面粘贴代码:
因为,我的编译器说他如下:
addTime.cpp: In function 'int main()':
addTime.cpp:20:9: error: expected ';' before 'time1'
20 | time time1, time2, timetot;
| ^~~~~~
| ;
addTime.cpp:27:40: error: 'time1' was not declared in this scope; did you mean 'timeb'?
27 | cout << "Hours : " ; cin >> time1.hours ; cout << endl;
| ^~~~~
| timeb
addTime.cpp:30:40: error: 'time2' was not declared in this scope; did you mean 'timeb'?
30 | cout << "Hours : " ; cin >> time2.hours ; cout << endl;
| ^~~~~
| timeb
addTime.cpp:39:9: error: 'timetot' was not declared in this scope; did you mean 'time_t'?
39 | timetot.hours = time1.hours + time2.hours ;
| ^~~~~~~
| time_t
我决定将结构定义带入 main() 函数的范围内,我这样做了,代码可以工作。我粘贴了下面的代码,您也可以将其复制粘贴到编译器中并运行它。这确认了我的代码没有语法错误。那么,为什么书本上写的代码可以工作,而当我做同样的事情时,它不起作用
#include <iostream>
using namespace std;
// This function basically calculates how much is carried over to the next quantitiy for eg. if seconds //in 122, it will return 2 which will be carried to minutes
int carry (int inp) {
int carry;
carry = inp / 60;
return carry;
};
int main(){
struct time {
int hours;
int minutes;
int seconds;
};
char ques = 'y';
time time1, time2, timetot;
do {
cout << "Want to add time (y/n) ? : "; cin >> ques;
if (ques != 'y') break;
cout << "Hours : " ; cin >> time1.hours ; cout << endl;
cout << "Minutes : " ; cin >> time1.minutes; cout << endl;
cout << "Seconds : " ; cin >> time1.seconds; cout << endl;
cout << "Hours : " ; cin >> time2.hours ; cout << endl;
cout << "Minutes : " ; cin >> time2.minutes; cout << endl;
cout << "Seconds : " ; cin >> time2.seconds; cout << endl;
if (time1.seconds >=60) { time1.minutes += carry(time1.seconds); time1.seconds %= 60; }
if (time1.minutes >=60) { time1.hours += carry(time1.minutes); time1.minutes %= 60; }
if (time2.seconds >=60) { time2.minutes += carry(time2.seconds); time2.seconds %= 60; }
if (time2.minutes >=60) { time2.hours += carry(time2.minutes); time2.minutes %= 60; }
timetot.hours = time1.hours + time2.hours ;
timetot.minutes = time1.minutes + time2.minutes ;
timetot.seconds = time1.seconds + time2.seconds ;
if (time1.seconds >=60) { time1.minutes += carry(time1.seconds); }
if (time1.minutes >=60) { time1.hours += carry(time1.minutes); }
if (time2.seconds >=60) { time2.minutes += carry(time2.seconds); }
if (time2.minutes >=60) { time2.hours += carry(time2.minutes); }
cout << "Tot Hours" << timetot.hours << endl;
cout << "Tot minutes" << timetot.minutes << endl;
cout << "Tot seconds" << timetot.seconds << endl;
}
while (ques=='y') ;
return 0;
};
正如你所看到的,我所做的只是在main()函数中定义结构,程序开始工作。我错过了什么?请帮帮我。
答: 暂无答案
评论
using namespace std;
time