本文介绍了for循环不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! #include < iostream > #include < iomanip > #include C1A7E1_MyTime.h 使用 命名空间标准; MyTime * DetermineElapsedTime( const MyTime * tp1, const MyTime * tp2) ; int main() { for ( int loop_count = 0 ; loop_count< 3 ; loop_count ++ ) { cout<< 输入时间点1,空格分隔:\ n; cin>> tm1.hours>> tm1.minutes>> tm1.seconds; cout<< 输入时间点1,空格分隔:\ n; cin>> tm2.hours>> tm2.minutes>> tm2.seconds; cout<< 输入功能!; DetermineElapsedTime( const tm1, const tm2)} const MyTime tm1 = { 2 , 0 , 0 }; const MyTime tm2 = { 0 , 0 , 0 }; MyTime time_dif = * DetermineElapsedTime(& tm1,& tm2); cout<< 从;经过的时间 cout<< setfill(' 0')<< setw( 2 )<< tm1.hours<< :; cout<< setfill(' 0')<< setw( 2 )<< tm1.minutes<< :; cout<< setfill(' 0')<< setw( 2 )<< tm1.seconds; cout<< to; cout<< setfill(' 0')<< setw( 2 )<< tm2.hours<< :; cout<< setfill(' 0')<< setw( 2 )<< tm2.minutes<< :; cout<< setfill(' 0')<< setw( 2 )<< tm2.seconds; cout<< 是; cout<< time_dif.hours<< :<< time_dif.minutes<< :<< time_dif.seconds<< \ n; } C1A7E1_MyTime.h #ifndef Class_Test_C1A7E1_MyTime_h #define Class_Test_C1A7E1_MyTime_h 使用 命名空间标准; struct MyTime { int 小时,分钟,秒; }; / * 请勿更改此* / #endif C1A7E1_DetermineElapsedTime.cpp #include < iostream > #include C1A7E1_MyTime.h 使用 命名空间标准; MyTime * DetermineElapsedTime( const MyTime * tp1, const MyTime * tp2) { static MyTime return_time; MyTime testy1,testy2; testy1 = * tp1; testy2 = * tp2; long time_point_dif; time_point_dif =( long (testy1.hours)* 3600 + long (testy1.minutes)* 60 + long (testy1.seconds )) - \ ( long (testy2.hours)* 3600 + long (testy2.minutes)* 60 + long ( testy2.seconds)); return_time.hours = int (time_point_dif / 3600 ); time_point_dif - = return_time.hours * 3600 ; return_time.minutes = int (time_point_dif / 60 ); time_point_dif - = return_time.minutes * 60 ; return_time.seconds = int (time_point_dif); return (& return_time); } 我的尝试: 我试图创建数据类型int tm1,tm2?不知道为什么我收到的错误不在范围内。解决方案 const MyTime tm1 = {2,0,0}; const MyTime tm2 = {0,0,0}; 在for循环块之后声明。移到for循环之前再试一次。 引用:我试图创建数据类型int tm1,tm2?不知道为什么我收到的错误不在范围内。 编译器不满意,因为你在创建它们之前使用 tm1 和 tm2 。 原则上,在声明之前你不能使用它。 [更新] 引用:是的,但是我该怎么办呢?我试图在手前声明变量但它仍然不起作用? Don不要将它们声明为 const 或不要写入它们。 #include <iostream>#include <iomanip>#include "C1A7E1_MyTime.h"using namespace std;MyTime *DetermineElapsedTime(const MyTime *tp1, const MyTime *tp2);int main(){ for (int loop_count = 0; loop_count < 3; loop_count++) { cout << "Enter time point one, space separated:\n"; cin >> tm1.hours >> tm1.minutes >> tm1.seconds; cout << "Enter time point one, space separated:\n"; cin >> tm2.hours >> tm2.minutes >> tm2.seconds; cout << "entering function!"; DetermineElapsedTime(const tm1, const tm2) } const MyTime tm1 = { 2, 0, 0}; const MyTime tm2 = { 0, 0, 0}; MyTime time_dif = *DetermineElapsedTime(&tm1, &tm2); cout << "The time elapsed from "; cout << setfill('0') << setw(2) << tm1.hours << ":"; cout << setfill('0') << setw(2) << tm1.minutes << ":"; cout << setfill('0') << setw(2) << tm1.seconds; cout << " to "; cout << setfill('0') << setw(2) << tm2.hours << ":"; cout << setfill('0') << setw(2) << tm2.minutes << ":"; cout << setfill('0') << setw(2) << tm2.seconds; cout << " is "; cout << time_dif.hours << ":" << time_dif.minutes << ":" << time_dif.seconds << "\n";}C1A7E1_MyTime.h#ifndef Class_Test_C1A7E1_MyTime_h#define Class_Test_C1A7E1_MyTime_husing namespace std;struct MyTime { int hours, minutes, seconds; }; /* do not change this */#endifC1A7E1_DetermineElapsedTime.cpp#include <iostream>#include "C1A7E1_MyTime.h"using namespace std;MyTime *DetermineElapsedTime(const MyTime *tp1, const MyTime *tp2){ static MyTime return_time; MyTime testy1, testy2; testy1 = *tp1; testy2 = *tp2; long time_point_dif; time_point_dif = (long(testy1.hours) * 3600 + long(testy1.minutes) * 60 + long(testy1.seconds)) - \ (long(testy2.hours) * 3600 + long(testy2.minutes) * 60 + long(testy2.seconds)); return_time.hours = int(time_point_dif / 3600); time_point_dif -= return_time.hours * 3600; return_time.minutes = int(time_point_dif / 60); time_point_dif -= return_time.minutes * 60; return_time.seconds = int(time_point_dif); return(&return_time);}What I have tried:I tried to create data type int tm1, tm2? Not sure why I am getting an error not in scope. 解决方案 const MyTime tm1 = { 2, 0, 0};const MyTime tm2 = { 0, 0, 0};are declared after the for loop block. Move to before the for loop and try again.Quote:I tried to create data type int tm1, tm2? Not sure why I am getting an error not in scope. The compiler is unhappy because you use tm1 and tm2 before creating them.By principle, you can't use something before declaring it.[Update]Quote:Yes but what do I do about it I have tried to declare the variable before hand and it still does not work?Don't declare them as const or don't write into them. 这篇关于for循环不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 06:59