Zhengliang
Zhengliang

Reputation: 3

C++ compile errors when using template in unordered_map

I am trying to use unordered_map in my project. The code related to my problem is

#include <bitset>
#include <unordered_map>

struct Node
{};

struct Node_IB
{};

struct Grid
{
    std::unordered_map< std::bitset<64>, Node> grid;
};

struct Grid_IB
{
    std::unordered_map< std::bitset<64>, Node_IB> grid;
};


template <class T_grid, class T_node>
void func1(T_grid &grid_ptr, T_node node_temp);

int main()
{
    Grid grid_ptr1;
    Grid_IB grid_ptr2;

    int ilevel = 1;
    if (ilevel == 1)
    {
        Node_IB node_temp;
        func1(grid_ptr1, node_temp);
    }
    else
    {
        Node node_temp;
        func1(grid_ptr2, node_temp);
    }
}

template <class T_grid, class T_node>
void func1(T_grid &grid_ptr, T_node node_temp)
{
    std::bitset<64> key = 0;
    grid_ptr.grid.insert(make_pair(key, node_temp));
} 

The error is "error C2664: 'std::_List_iterator>> std::_Hash,_Alloc,false>>::insert(std::_List_const_iterator>>,const std::pair &)': cannot convert argument 1 from 'std::pair,Node>' to 'std::pair &&'"

I tried to call func1 in another function (func_manager) and met the same problem. I am confused about template instantiation? Is there any limitation of this procedure?

#include <bitset>
#include <unordered_map>

struct Node
{};

struct Node_IB
{};

struct Grid
{
    std::unordered_map< std::bitset<64>, Node> grid;
};

struct Grid_IB
{
    std::unordered_map< std::bitset<64>, Node_IB> grid;
};


template <class T_grid, class T_node>
void func1(T_grid &grid_ptr, T_node node_temp);

template <class T_gr>
void func_manager(T_gr &grid_ptr, int ilevel);

int main()
{
    Grid_IB grid_ptr1;
    Grid grid_ptr2;

    int ilevel = 1;
    if (ilevel == 1)
    {
        func_manager<Grid_IB>(grid_ptr1, ilevel);
    }
    else
    {
        func_manager<Grid>(grid_ptr2, ilevel);
    }

}

template <class T_gr>
void func_manager(T_gr &grid_ptr, int ilevel)
{

    if (ilevel == 1)
    {
        Node_IB node_temp;
        func1(grid_ptr, node_temp);
    }
    else
    {
        Node node_temp;
        func1(grid_ptr, node_temp);
    }

}


template <class T_grid, class T_node>
void func1(T_grid &grid_ptr, T_node node_temp)
{
    std::bitset<64> key = 0;
    grid_ptr.grid.insert(make_pair(key, node_temp));
}

I am new to code development. Sorry for any confusing information.

Could anyone help me with this?

Thank you

Upvotes: 0

Views: 118

Answers (1)

TonySalimi
TonySalimi

Reputation: 8427

Change your main function like this and it will be compiled.

struct Node
{
};

struct Node_IB
{
};

struct Grid
{
   std::unordered_map< std::bitset<64>, Node> grid;
};

struct Grid_IB
{
   std::unordered_map< std::bitset<64>, Node_IB> grid;
};


template <class T_grid, class T_node>
void func1(T_grid &grid_ptr, T_node node_temp);

template <class T_gr, class T_node>
void func_manager(T_gr &grid_ptr, T_node & node_temp, int ilevel);

int main()
{
   Grid_IB grid_ptr1;
   Grid grid_ptr2;

   Node_IB node_ib;
   Node  node;

   int ilevel = 1;
   if (ilevel == 1)
   {
      func_manager<Grid_IB, Node_IB>(grid_ptr1, node_ib, ilevel);
   }
   else
   {
      func_manager<Grid, Node>(grid_ptr2, node, ilevel);
   }

}

template <class T_gr, class T_node>
void func_manager(T_gr &grid_ptr, T_node & node_temp, int ilevel)
{

   if (ilevel == 1)
   {
      func1(grid_ptr, node_temp);
   }
   else
   {
      func1(grid_ptr, node_temp);
   }

}


template <class T_grid, class T_node>
void func1(T_grid &grid_ptr, T_node node_temp)
{
   std::bitset<64> key = 0;
   grid_ptr.grid.insert(std::make_pair(key, node_temp));
}

Upvotes: 1

Related Questions