创建类对象的无序映射

时间:2015-04-16 01:13:34

标签: c++ class object unordered-map

我创建了一个函数,它应该从.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>          ]

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

1 个答案:

答案 0 :(得分:1)

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

您需要制作默认构造函数,或者使用insertfind代替。