gpt4 book ai didi

c++ - 创建类对象的无序映射

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

我创建了一个函数,该函数应该从 .txt 文件中读取信息,将其存储在类对象中,然后将其存储在哈希表中。我已经测试了信息的阅读,它工作正常。当我尝试将信息存储在对象中或将对象存储在哈希表中时,问题就出现了。该函数的代码如下所示:

#include<iostream>
#include<fstream>
#include<string>
#include <sstream>
#include <unordered_map>
#include "car.h"
using namespace std;



unordered_map <string, car> allCars;

void readInput()
{

int arr[24];
int x = 0;
int numberofCases,milemarker;
string line,datetime,enterexit,license;


ifstream File;
File.open("input.txt");
File >> numberofCases;


while (x <= numberofCases)
{
for (int i = 0; i < 24; i++)
{
File >> arr[i];

}

getline(File, line);
while (getline(File, line))
{
if (line.empty())
{
x++;
for (int i = 0; i < 24; i++)
{
File >> arr[i];
}
for (int i = 0; i < 24; i++)
{
cout << arr[i] << " ";
}
cout << endl;
getline(File, line);
getline(File, line);
stringstream lineStream(line);
lineStream >> license >> datetime >> enterexit >> milemarker;
string sub = datetime.substr(6, 2);
int hour = stoi(sub);
if (allCars.count(license))
{
(allCars[license]).settollCost(arr[hour]);
if (enterexit == "enter")
{
(allCars[license]).setcarEnter(milemarker);
}
else if (enterexit == "exit")
{
(allCars[license]).setcarExit(milemarker);
(allCars[license]).calculateMiles;
(allCars[license]).calculateBill; // calculates bill for trip
(allCars[license]).printInfo;
}

}
else if (!allCars.count(license))
{
car car1(license);
allCars[license] = car1;
}
cout << license << endl << datetime << endl << enterexit << endl << milemarker << endl << hour << endl;
}
else
{
stringstream lineStream(line);
lineStream >> license >> datetime >> enterexit >> milemarker;
string sub = datetime.substr(6, 2);
int hour = stoi(sub);
if (allCars.count(license))
{
(allCars[license]).settollCost(arr[hour]);
if (enterexit == "enter")
{
(allCars[license]).setcarEnter(milemarker);
}
else if (enterexit == "exit")
{
(allCars[license]).setcarExit(milemarker);
(allCars[license]).calculateMiles;
(allCars[license]).calculateBill; // calculates bill for trip
(allCars[license]).printInfo;
}

}
else if (!allCars.count(license))
{
car car1(license);
allCars[license] = car1;
}
cout << license << endl << datetime << endl << enterexit << endl << milemarker << endl << hour << endl;
for (int i = 0; i < 24; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
}
}

}

我从构建中得到的输出如下:

c:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(746): error C2512: 'car' : no appropriate default constructor available
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(762) : see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<std::tuple<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &>,std::tuple<>,0,>(_Tuple1 &,_Tuple2 &,std::_Arg_idx<0>,std::_Arg_idx<>)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=car
1> , _Tuple1=std::tuple<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &>
1> , _Tuple2=std::tuple<>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(762) : see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<std::tuple<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &>,std::tuple<>,0,>(_Tuple1 &,_Tuple2 &,std::_Arg_idx<0>,std::_Arg_idx<>)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=car
1> , _Tuple1=std::tuple<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &>
1> , _Tuple2=std::tuple<>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600) : see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char>>&,>(std::piecewise_construct_t,std::tuple<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &>,std::tuple<>)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=car
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600) : see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char>>&,>(std::piecewise_construct_t,std::tuple<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &>,std::tuple<>)' being compiled
1> with
1> [
1> _Kty=std::string
1> , _Ty=car
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,const std::piecewise_construct_t&,_Ty,std::tuple<>>(_Objty *,const std::piecewise_construct_t &,_Ty &&,std::tuple<> &&)' being compiled
1> with
1> [
1> _Other=std::_List_node<std::pair<const std::string,car>,void *>
1> , _Objty=std::pair<const std::string,car>
1> , _Ty=std::tuple<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &>
1> ]

整个输出实际上更长,但几乎所有输出都与上面发布的相同。我不知道这意味着什么,也不知道我到底做错了什么,所以如有任何澄清,我们将不胜感激。如果您需要更多信息,例如类定义或头文件,我可以提供;只是不想让问题过载。

最佳答案

正如 Nemo 所说,您的 car 类很可能没有默认构造函数,which the operator[] of unordered_map requires .

您要么需要创建默认构造函数,要么使用insertfind 代替。

关于c++ - 创建类对象的无序映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29663678/

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