我已经创建了一个类,在这个类中,我使用了一个函数将标准时间转换为军事时间,现在我正尝试在这个类中的另一个函数中使用该函数。但是每次我尝试编译的时候,它都会返回这个作用域中没有声明的。
以下是我的主要内容:
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中。
发布于 2019-12-04 09:00:12
您的standardToMilitary是一个非静态成员函数,因此它需要一个对象实例来调用它:
Appointment a;
//...
a.standardToMilitary(/*...*/);如果您希望在不使用对象的情况下调用它,则将其设置为独立函数(将其移出类定义),或者在类中将其声明为static。
https://stackoverflow.com/questions/59167224
复制相似问题