我一直在为我的类(class)编写这段代码,但我不确定这些错误是什么意思或如何解决这些错误。另外,我不确定下一步是什么或如何完成程序。我只使用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

我的任务是:
parking 库收取最多3个小时的最低 parking 费$ 2.00。如果超过三个小时,则车库每小时收取额外的$ 0.50的费用。在任何给定的24小时内,最高费用为10.00美元。 parking 时间超过24小时的人每天将支付$ 8.00。
编写一个计算并打印 parking 费的程序。程序的输入是汽车进入 parking 场的日期和时间,以及同一辆汽车离开 parking 场的日期和时间。两个输入均采用以下格式: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;
}

最佳答案

看起来您从未分别定义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;
}

关于c++ - 使用atoi和error:未解析的外部符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10060139/

10-09 13:31