首页 > 解决方案 > 创建无序地图实例时出错

问题描述

创建 unordered_map<string, object> 的实例时出现错误。在添加 unordered_map 之前一切正常。很抱歉未定义的描述,但我无法从调试中提取主要错误。

我试图将链接上的对象替换为 unordered_map 初始化中的类型,但这没有帮助。

====================[ Build | Shop | Debug ]====================================
"C:\Program Files\JetBrains\CLion 2019.3.5\bin\cmake\win\bin\cmake.exe" --build C:\Users\Alexey\CLionProjects\Shop\cmake-build-debug --target Shop -- -j 4
[ 50%] Building CXX object CMakeFiles/Shop.dir/main.cpp.obj
In file included from C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/hashtable_policy.h:34,
                 from C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/hashtable.h:35,
                 from C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/unordered_map:46,
                 from C:\Users\Alexey\CLionProjects\Shop\main.cpp:3:
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/tuple: In instantiation of 'std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&}; unsigned int ..._Indexes1 = {0}; _Args2 = {}; unsigned int ..._Indexes2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = Product]':
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/tuple:1657:63:   required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&}; _Args2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = Product]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h:136:4:   required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const std::__cxx11::basic_string<char>, Product>; _Args = {const std::piecewise_construct_t&, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true>]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/alloc_traits.h:475:4:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const std::__cxx11::basic_string<char>, Product>; _Args = {const std::piecewise_construct_t&, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true>; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true> >]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/hashtable_policy.h:2082:36:   required from 'std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true>]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/hashtable_policy.h:736:8:   required from 'std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type&&) [with _Key = std::__cxx11::basic_string<char>; _Pair = std::pair<const std::__cxx11::basic_string<char>, Product>; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Product> >; _Equal = std::equal_to<std::__cxx11::basic_string<char> >; _H1 = std::hash<std::__cxx11::basic_string<char> >; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = Product; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = std::__cxx11::basic_string<char>]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/unordered_map.h:981:20:   required from 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type&&) [with _Key = std::__cxx11::basic_string<char>; _Tp = Product; _Hash = std::hash<std::__cxx11::basic_string<char> >; _Pred = std::equal_to<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Product> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = Product; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = std::__cxx11::basic_string<char>]'
C:\Users\Alexey\CLionProjects\Shop\main.cpp:42:32:   required from here
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/tuple:1668:70: error: no matching function for call to 'Product::Product()'
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
C:\Users\Alexey\CLionProjects\Shop\main.cpp:13:5: note: candidate: 'Product::Product(std::__cxx11::string, std::__cxx11::string)'
     Product(string code, string name) : code(code), name(name){
     ^~~~~~~
C:\Users\Alexey\CLionProjects\Shop\main.cpp:13:5: note:   candidate expects 2 arguments, 0 provided
C:\Users\Alexey\CLionProjects\Shop\main.cpp:7:7: note: candidate: 'Product::Product(const Product&)'
 class Product{
       ^~~~~~~
C:\Users\Alexey\CLionProjects\Shop\main.cpp:7:7: note:   candidate expects 1 argument, 0 provided
C:\Users\Alexey\CLionProjects\Shop\main.cpp:7:7: note: candidate: 'Product::Product(Product&&)'
C:\Users\Alexey\CLionProjects\Shop\main.cpp:7:7: note:   candidate expects 1 argument, 0 provided
In file included from C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/hashtable_policy.h:34,
                 from C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/hashtable.h:35,
                 from C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/unordered_map:46,
                 from C:\Users\Alexey\CLionProjects\Shop\main.cpp:3:
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/tuple: In instantiation of 'std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}; unsigned int ..._Indexes1 = {0}; _Args2 = {}; unsigned int ..._Indexes2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = Product]':
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/tuple:1657:63:   required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}; _Args2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = Product]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h:136:4:   required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const std::__cxx11::basic_string<char>, Product>; _Args = {const std::piecewise_construct_t&, std::tuple<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true>]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/alloc_traits.h:475:4:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const std::__cxx11::basic_string<char>, Product>; _Args = {const std::piecewise_construct_t&, std::tuple<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true>; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true> >]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/hashtable_policy.h:2082:36:   required from 'std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, Product>, true>]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/hashtable_policy.h:711:8:   required from 'std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](const key_type&) [with _Key = std::__cxx11::basic_string<char>; _Pair = std::pair<const std::__cxx11::basic_string<char>, Product>; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Product> >; _Equal = std::equal_to<std::__cxx11::basic_string<char> >; _H1 = std::hash<std::__cxx11::basic_string<char> >; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = Product; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = std::__cxx11::basic_string<char>]'
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/unordered_map.h:977:20:   required from 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = Product; _Hash = std::hash<std::__cxx11::basic_string<char> >; _Pred = std::equal_to<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Product> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = Product; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = std::__cxx11::basic_string<char>]'
C:\Users\Alexey\CLionProjects\Shop\main.cpp:48:29:   required from here
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/tuple:1668:70: error: no matching function for call to 'Product::Product()'
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
C:\Users\Alexey\CLionProjects\Shop\main.cpp:13:5: note: candidate: 'Product::Product(std::__cxx11::string, std::__cxx11::string)'
     Product(string code, string name) : code(code), name(name){
     ^~~~~~~
C:\Users\Alexey\CLionProjects\Shop\main.cpp:13:5: note:   candidate expects 2 arguments, 0 provided
C:\Users\Alexey\CLionProjects\Shop\main.cpp:7:7: note: candidate: 'Product::Product(const Product&)'
 class Product{
       ^~~~~~~
C:\Users\Alexey\CLionProjects\Shop\main.cpp:7:7: note:   candidate expects 1 argument, 0 provided
C:\Users\Alexey\CLionProjects\Shop\main.cpp:7:7: note: candidate: 'Product::Product(Product&&)'
C:\Users\Alexey\CLionProjects\Shop\main.cpp:7:7: note:   candidate expects 1 argument, 0 provided
mingw32-make.exe[3]: *** [CMakeFiles\Shop.dir\build.make:62: CMakeFiles/Shop.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/Shop.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/Shop.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: Shop] Error 2

代码:

#include <iostream>
#include <vector>
#include <unordered_map>
#include <fstream>
using namespace std;

class Product{
    string code;
    string name;
    size_t price = 0;
    size_t count = 0;
public:
    Product(string code, string name) : code(code), name(name){
    }
    void Load(size_t count, size_t price){
        this->price = price;
        this->count = count;
    }
    void Get(size_t count){
        if(count > this->count)
            throw("You would like buy more product then in shop!");
        else
            this->count -= count;
    }
    string GetCode(){
        return this->code;
    }
    size_t GetPrice(){
        return this->price;
    }

};

class Shop{
    string code;
    string name;
    unordered_map<string, Product> products;
public:
    Shop(string code, string name):code(code), name(name){}
    void AddProducts(Product prod, size_t count, size_t price){
        products.insert(make_pair(prod.GetCode(), prod)); //[prod.GetCode()] = prod;
        products[prod.GetCode()].Load(count, price);
    }
    size_t GetPrice(string code){
        //isInstance?
        if(products.count(code) == 0)
            throw("Object isn't Instance!");
        return products[code].GetPrice();
    }
    string GetName(){
        return this->name;
    }

};

class Person{
    size_t money;
    vector<Shop> visible_shops;
public:
    Person(size_t money): money(money){}
    void AddShop(Shop shop){
        visible_shops.push_back(shop);
    }
    string FindShopWithCheepestProduct(string code){
        vector<Shop>::iterator iter = visible_shops.begin();
        size_t minPrice = iter->GetPrice(code);
        string ShopName = iter->GetName();
        for(; iter != visible_shops.end(); iter++){
            if(iter->GetPrice(code) < minPrice){
                minPrice = iter->GetPrice(code);
                ShopName = iter->GetName();
            }
        }
        return ShopName; 
    }
};

int main() {
    Shop first = Shop("123", "Икея");
    Product notebooks = Product("54637284", "Блокнот");
    first.AddProducts(notebooks, 100, 30);
    Person person = Person(100);
    person.AddShop(first);
    cout << person.FindShopWithCheepestProduct("54637284");
    return 0;
}

标签: c++clionunordered-map

解决方案


推荐阅读