gpt4 book ai didi

c++ - 使用 atoi 和错误 :unresolved external symbol

转载 作者:行者123 更新时间:2023-11-30 02:04:51 28 4
gpt4 key购买 nike

我一直在为我的类(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 库收取 2.00 美元的最低 parking 费,最多可停放三个小时。车库对超过三小时的每小时或部分时间额外收费 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 和错误 :unresolved external symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10060139/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com