使用atoi和错误:未解析的外部符号

时间:2012-04-08 03:23:34

标签: c++ visual-c++

我一直在为我的班级编写代码,我只是不确定错误是什么意思或如何修复它们。此外,我不确定下一步是什么或如何完成该计划。我只使用了一个月的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美元的停车费可停放长达3小时。车库每小时额外收费0.50美元,或每小时超过3小时。任何给定的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;
}

1 个答案:

答案 0 :(得分: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;
}