简体   繁体   English

在 function 下调用 function 时找不到标识符

[英]Identifier not found when calling a function under a function

I trying to develop a program that displays a 12 hour and 24 hour clock at the same time.我试图开发一个同时显示 12 小时和 24 小时时钟的程序。 But whenever I compile, I get a build error saying 'GetAM_PM': identifier not found.但是每当我编译时,我都会收到一个构建错误,说“GetAM_PM”:找不到标识符。 I get this error on line 26 in spite of using the same variable from my function parameter.尽管使用了与我的 function 参数相同的变量,但我在第 26 行收到此错误。 What could be the root of this problem?这个问题的根源可能是什么? Here is my code:这是我的代码:

#include <iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

//converting it into 12 hour format
int TwelveHourFormat(int twelve_hours) {
return (twelve_hours == 0 || twelve_hours == 12) ? 12 : 
twelve_hours % 12;
}


//printing the 12 hour format
void Display_12_HourFormat(int seconds, int minutes, int 
twelve_hours) {

cout.fill('0');
cout << TwelveHourFormat(twelve_hours) << ":" << minutes << ":" 
<< seconds << " " << GetAM_PM(twelve_hours);
}

//printing the 24 hour format
void Display_24_HourFormat(int seconds, int minutes, int 
twenty_four_hours) {
cout.fill('0');
cout << twenty_four_hours << ":" << minutes << ":" << seconds;
}
    
void AddHour(int hour) {

hour = (hour + 1) % 24;

}
    
void AddMinute(int hour, int min) {
if (min == 59) {
    AddHour(hour);
}
min = (min + 1) % 60;
}

void AddSecond(int min, int sec) {

if (sec == 59) {
    AddMinute(min, sec);
}
sec = (sec + 1) % 60;
}

// function return AM/PM respect to hour of time
string GetAM_PM(int twelve_hours) {

return twelve_hours >= 12 ? "PM" : "AM";
}


// This method prints the menu options
void DisplayMenu() {
cout << "Chada Tech Clocks Menu" << endl;
cout << "[1] Add one hour" << endl;
cout << "[2] Add one minute" << endl;
cout << "[3] Add one second" << endl;
cout << "[4] Exit program" << endl;

}

 int main()
{  
int seconds, minutes, hours;

//obtains current time in seconds
time_t total_seconds = time(0); 

//getting values of seconds, minutes and hours
struct tm ct;
localtime_s(&ct, &total_seconds);

seconds = ct.tm_sec;
minutes = ct.tm_min;
hours = ct.tm_hour;

    // Variable declared
    int option;
    do
    {
    // DisplayMenu function is called
    DisplayMenu();
    
    cin >> option;
    // If user input is 1, Clock function is called
    if (option == 1) {

        TwelveHourFormat(hours);
        AddHour(hours);
        GetAM_PM(hours);
        Display_12_HourFormat(seconds, minutes, hours);
        Display_24_HourFormat(seconds, minutes, hours);
        
    }
    // If the option is 2, the Clock function is called
    else if (option == 2) {

        AddMinute(minutes, seconds);
        GetAM_PM(hours);

    }
    // If the option is 3, the Clock function is called
    else if (option == 3) {

        AddSecond(minutes, seconds);
        GetAM_PM(hours);
        
    }
    // If the option is 4, exit message prints and application 
    stops running
    else if (option == 4) {

        cout << "You have exited the application.";
        break;
    }
    else {
        cout << "You have entered an invalid input." << endl;
    }
} while (option != 4);
}

Did you forward declare GetAM_PM above main?您是否在 main 上方转发声明 GetAM_PM?

string GetAM_PM(int);

Just move these lines to the beginning, before any other function:只需将这些行移到开头,在任何其他 function 之前:

// function return AM/PM respect to hour of time
string GetAM_PM(int twelve_hours) {

return twelve_hours >= 12 ? "PM" : "AM";
}

It is not this case, but if you end up with circular dependency, try to declare the methods in ah or forward declare the methods in the code.不是这种情况,但是如果最终出现循环依赖,请尝试在 ah 中声明方法或在代码中前向声明方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM