1

我一直在为我的班级编写这段代码,但我不确定这些错误是什么意思或如何修复它们。另外,我不确定下一步是什么或如何完成该程序。我只用了一个月的 C++,对其中的任何一个都不是很熟悉。先感谢您!

error LNK2019: unresolved external symbol "int __cdecl parseDate(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?parseDate@@YAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
: fatal error LNK1120: 1 unresolved externals

我的任务是:
停车场收取 2.00 美元的最低停车费,最多可停放三个小时。超过三个小时,车库每小时或部分每小时额外收费 0.50 美元。任何给定 24 小时期间的最高费用为 10.00 美元。停车超过 24 小时的人每天将支付 8.00 美元。
编写一个计算并打印停车费的程序。程序的输入是汽车进入停车场的日期和时间,以及同一辆车离开停车场的日期和时间。两个输入的格式为:YY/MM/DD hh:mm

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
using namespace std;

string startDateString;
string endDateString;
string dateStr;
int parseDate( string dateStr );

int main ()

{

string enter_date;
string enter_time;
string exit_date;
string exit_time;
cout << "Please enter the date and time the car is entering "<< endl
    << "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin,dateStr);//cin >> enter_date >> enter_time;

cout<< "Please enter the date and time the car is exiting "<< endl
    << "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin,dateStr);//cin >> exit_date >> exit_time;


{
    // Format: YY/MM/DD hh:mm
    int year  = atoi( dateStr.substr( 0, 2 ).c_str() );
    int month = atoi( dateStr.substr( 3, 2 ).c_str() );
    int day   = atoi( dateStr.substr( 6, 2 ).c_str() );
    int hour  = atoi( dateStr.substr( 9, 2 ).c_str() );
    int min   = atoi( dateStr.substr( 12, 2 ).c_str() );

    // Now calculate no. of mins and return this
    int totalMins = 0;
    totalMins += ( year * 365 * 24 * 60 ); 
    totalMins += ( month * 30 * 24 * 60 ); 
    totalMins += ( day * 24 * 60 );        
    totalMins += ( hour * 60 );
    totalMins += ( min );

    return totalMins;
}
int startTime = parseDate( startDateString );
int endTime   = parseDate( endDateString );
int elapsedTime = endTime - startTime; // elapsedTime is no. of minutes parked

return 0;
}
4

1 回答 1

4

看起来您从未parseDate()单独定义,而是将其放入您的main(). 我认为你需要拿出:

{
    // Format: YY/MM/DD hh:mm
    int year  = atoi( dateStr.substr( 0, 2 ).c_str() );
    int month = atoi( dateStr.substr( 3, 2 ).c_str() );
    int day   = atoi( dateStr.substr( 6, 2 ).c_str() );
    int hour  = atoi( dateStr.substr( 9, 2 ).c_str() );
    int min   = atoi( dateStr.substr( 12, 2 ).c_str() );

    // Now calculate no. of mins and return this
    int totalMins = 0;
    totalMins += ( year * 365 * 24 * 60 ); 
    totalMins += ( month * 30 * 24 * 60 ); 
    totalMins += ( day * 24 * 60 );        
    totalMins += ( hour * 60 );
    totalMins += ( min );

    return totalMins;
}

并将其放在代码末尾的单独函数中:

int parseDate (string dateStr) 
{
    // Format: YY/MM/DD hh:mm
    int year  = atoi( dateStr.substr( 0, 2 ).c_str() );
    int month = atoi( dateStr.substr( 3, 2 ).c_str() );
    int day   = atoi( dateStr.substr( 6, 2 ).c_str() );
    int hour  = atoi( dateStr.substr( 9, 2 ).c_str() );
    int min   = atoi( dateStr.substr( 12, 2 ).c_str() );

    // Now calculate no. of mins and return this
    int totalMins = 0;
    totalMins += ( year * 365 * 24 * 60 ); 
    totalMins += ( month * 30 * 24 * 60 ); 
    totalMins += ( day * 24 * 60 );        
    totalMins += ( hour * 60 );
    totalMins += ( min );

    return totalMins;
}
于 2012-04-08T05:12:40.903 回答