首页 > 解决方案 > 错误:未在此范围内声明“standardToMilitary”

问题描述

我创建了一个类,在该类中我使用一个函数将标准时间转换为军用时间,我现在尝试在该类中的另一个函数中使用该函数。但是每次我尝试编译时,它都会返回未在此范围内声明。

这是我的主要内容:

int main(int argc, char const *argv[]) { 
    ifstream in ; 
    string appData ; 
    vector<Appointment> agenda ;
    vector<Appointment> temp ; 
    in.open("agenda.txt") ; 

    if(in.fail()){
        cout << "file error" << endl ; 
    }

    while(!in.eof()){
        getline(in, appData) ; 
        if(!appData.empty()){
            agenda.push_back(appData) ;  
        } 
    }
    in.close() ; 


    if(strcmp(argv[1], "-ps") == 0){
        psFunc(agenda, temp) ; 
    } 
}

这是我的.cc文件,其中包含返回错误的函数:

void psFunc(vector<Appointment> mainVector, vector<Appointment> temp){
    int cmpr = 0 ; 
    string temp1 ; 
    string temp2 ; 
    while(mainVector.size() != temp.size()){
        for(size_t i = 0; i < mainVector.size(); i++){
            temp1 = mainVector.at(cmpr).getTime() ; 
            temp2 = mainVector.at(i).getTime() ; 
            if(standardToMilitary(temp1) < standardToMilitary(temp2)){
                continue ;
            } else if(standardToMilitary(temp1) > standardToMilitary(temp2)){
                cmpr = i ; 
            }
        }
        temp.push_back(temp1) ; 
        mainVector.erase(mainVector.begin() + cmpr) ; 
    }   
    cout << "Date" << setw(8) << "Title" << setw(30) << "Time" << setw(10) << "Duration" ; 
    cout << "----------------------------------------------------------------------------------------" ; 
    for(size_t j = 0; j < temp.size(); j++){
        cout << temp.at(j).getDate() + " " + temp.at(j).getTitle() << endl ; 
    }
}

这是standardToMilitary()

int Appointment::standardToMilitary(string time){
    //reads the line for an A or a for AM and the opposite for PM, it then substr to find the hours/minutes and compiles a string
    string militaryT ; 
    int returnTime ; 
    nospaces(time) ; 
    // this section is for times with single digit hours
    if(time.length() == 6){
        if(time.substr(4, 1) == "A" || time.substr(4, 1) == "a"){
            militaryT += time[0] ;  
            militaryT += time.substr(2,2) ;
            returnTime = stoi(militaryT) ;
        } else if(time.substr(4, 1) == "P" || time.substr(4, 1) == "p"){
            militaryT += time[0] ; 
            militaryT += time.substr(2,2) ;
            returnTime = stoi(militaryT) + 1200 ;  
        }
    }
    // this section is for times with double digit hours 
    if(time.length() == 7){
        if(time.substr(0,2) == "12" && (time.substr(5,1) == "A" || time.substr(5,1) == "a")){
            militaryT += time.substr(3,2) ; 
            returnTime = stoi(militaryT) ; 
        } else if(time.substr(0,2) == "12" && (time.substr(5,1) == "P" || time.substr(5,1) == "p")){
            militaryT += "12" ; 
            militaryT += time.substr(3,2) ; 
            returnTime = stoi(militaryT) ;
        } else if(time.substr(5, 1) == "A" || time.substr(5, 1) == "a"){
            militaryT += time.substr(0,2) ;  
            militaryT += time.substr(3,2) ;
            returnTime = stoi(militaryT) ;
        } else if(time.substr(5,1) == "P" || time.substr(5,1) == "p"){
            militaryT += time.substr(0,2) ; 
            militaryT += time.substr(3,2) ;
            returnTime = stoi(militaryT) + 1200 ;  
        }
    }
    return returnTime ; 
}

我的程序正在从文件中读取行,将它们转换为Appointment类中的对象,然后将所有对象放入vector. psFunc应该按时间重新排序vector,我正在尝试使用该功能standardToMilitary()比较时间,然后查找最早的时间并将push_back()它们放入另一个vector.

标签: c++

解决方案


standardToMilitary是一个非静态成员函数,所以它需要一个对象实例来调用它:

Appointment a;
//...
a.standardToMilitary(/*...*/);

如果你想在没有对象的情况下调用它,那么让它成为一个独立的函数(将它移到类定义之外),或者static在类中声明它。


推荐阅读