Map problem

Jul 30, 2018 at 5:06pm

I have 2 classes, TexturedModel and Entity (with NO members or methods).

I write this code and it gives me a problem, for a reason I don't understand.

std::map<TexturedModel, std::vector<Entity>> entities;

void initializeBatch(TexturedModel& texturedModel) {

std::vector<Entity>& batch = entities[texturedModel]; //this line gives an error

}

why does this code give an error?
Jul 30, 2018 at 5:40pm
would've been faster if you actually gave the error. For custom keys in maps, they should define operator< , or make a custom functor comparator and use that in third template parameter of map initialization.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

class TexturedModel
{
public:
    TexturedModel() : id_(-1) {}
    TexturedModel(int id) : id_(id) {}
    virtual ~TexturedModel() {}

    bool operator< (const TexturedModel& other) const
    {
        return id_ < other.id_;
    }

private:
    int id_;
};

class Entity
{
public:
    Entity() {}
    virtual ~Entity() {}
};

void Blah(map<TexturedModel, vector<Entity>>& m, 
          TexturedModel& tex)
{
    vector<Entity>& batch = m[tex];    
}


int main() 
{
    map<TexturedModel, vector<Entity>> m
    {
        {TexturedModel(4), {Entity(), Entity()} },
        {TexturedModel(3), {Entity(), Entity(), Entity()} }
    };

    TexturedModel key(4);
    Blah(m, key);

    return 0;
}


Edit: the second, out-of-class, comparator functor option is harder to implement, because either the item you use for comparison would need to be public, or the comparator would need to be a friend and even then might get some errors as you fiddle around to fix constness and other issues.
Last edited on Jul 30, 2018 at 6:00pm
Jul 30, 2018 at 6:16pm
Ok, thanks for the solution. The error was really weird, here is the error:

visual studio\2017\community\vc\tools\msvc\14.14.26428\include\unordered_map(133): error C2280: 'std::hash<_Kty>::hash(const std::hash<_Kty> &)': attempting to reference a deleted function
1> with
1> [
1> _Kty=TexturedModel
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\type_traits(2392): note: compiler has generated 'std::hash<_Kty>::hash' here
1> with
1> [
1> _Kty=TexturedModel
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\unordered_map(132): note: while compiling class template member function 'std::unordered_map<TexturedModel,std::vector<Entity,std::allocator<_Ty>>,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,std::vector<_Ty,std::allocator<_Ty>>>>>::unordered_map(void)'
1> with
1> [
1> _Ty=Entity,
1> _Kty=TexturedModel
1> ]
1>c:\users\hassa\source\repos\c++\c++\src\main.cpp(12): note: see reference to function template instantiation 'std::unordered_map<TexturedModel,std::vector<Entity,std::allocator<_Ty>>,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,std::vector<_Ty,std::allocator<_Ty>>>>>::unordered_map(void)' being compiled
1> with
1> [
1> _Ty=Entity,
1> _Kty=TexturedModel
1> ]
1>c:\users\hassa\source\repos\c++\c++\src\main.cpp(12): note: see reference to class template instantiation 'std::unordered_map<TexturedModel,std::vector<Entity,std::allocator<_Ty>>,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,std::vector<_Ty,std::allocator<_Ty>>>>>' being compiled
1> with
1> [
1> _Ty=Entity,
1> _Kty=TexturedModel
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\type_traits(2392): note: 'std::hash<_Kty>::hash(const std::hash<_Kty> &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Conditionally_enabled_hash<_Kty,false>::_Conditionally_enabled_hash(const std::_Conditionally_enabled_hash<_Kty,false> &)'
1> with
1> [
1> _Kty=TexturedModel
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\type_traits(2376): note: 'std::_Conditionally_enabled_hash<_Kty,false>::_Conditionally_enabled_hash(const std::_Conditionally_enabled_hash<_Kty,false> &)': function was explicitly deleted
1> with
Jul 30, 2018 at 6:34pm
not sure on those exactly. Perhaps some of your destructor(s) weren't public or something and the STL containers had trouble dealing w/ them.
Topic archived. No new replies allowed.