在 C++ 中访问 int 映射到类对象时出错

Posted

技术标签:

【中文标题】在 C++ 中访问 int 映射到类对象时出错【英文标题】:Error while accessing map of int to class objects in c++ 【发布时间】:2020-02-23 11:00:47 【问题描述】:

我有这张地图map<int, PageTableEntry>> processmapram,其中键是整数,值是类对象。这就是向其中插入值的方式。

    for(i=0;i<lenDataSegment;i+=PG_SIZE)
    
            int ramloc = getFreeSpaceRAM();
            if(ramloc == -1)
            
                int swaploc = getFreeSwapSpace();
                bitmapSWAP[swaploc/PG_SIZE] = 1;
                PageTableEntry pte(swaploc, 1, 0);
                processmapram.insert(i, pte); \\ insert values to map
                strncpy(pSWap+swaploc, DataSeg+i, PG_SIZE);

            
            else
            
                bitmapRAM[ramloc/PG_SIZE] = 1;
                PageTableEntry pte(ramloc, 0, 1);
                processmapram.insert(i, pte); \\ insert values to map
                strncpy(pRAM+ramloc, DataSeg+i, PG_SIZE);
            
    

PageTableEntry 结构定义为

typedef struct PageTableEntry 
    int phyadd; 
    int flagswap;
    int flagram;
    PageTableEntry(int phyadd, int flagswap, int flagram)
    
            this->phyadd = phyadd;
            this->flagswap = flagswap;
            this->flagram = flagram;
    
 PageTableEntry;

现在当我在另一个函数中访问相同的函数时,我收到了一个错误:

int Process::readAddress(int virtualAddr, int lenToRead, char *buf)

        cout<<processmapram[virtualAddr].phyadd;

或者像这样

    PageTableEntry pte(-1,0,0);
    pte = mymap[virtualAddr];
    cout<<pte.phyadd;

错误:

In file included from /usr/include/c++/7/functional:54:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:71,
                 from Process.h:1,
                 from ProcessManager.h:1,
                 from Process.cpp:3:
/usr/include/c++/7/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 int&; long unsigned int ..._Indexes1 = 0; _Args2 = ; long unsigned int ..._Indexes2 = ; _T1 = const int; _T2 = PageTableEntry]’:
/usr/include/c++/7/tuple:1641:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = const int&; _Args2 = ; _T1 = const int; _T2 = PageTableEntry]’
/usr/include/c++/7/ext/new_allocator.h:136:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, PageTableEntry>; _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Tp = std::_Rb_tree_node<std::pair<const int, PageTableEntry> >]’
/usr/include/c++/7/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 int, PageTableEntry>; _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Tp = std::_Rb_tree_node<std::pair<const int, PageTableEntry> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const int, PageTableEntry> > >]’
/usr/include/c++/7/bits/stl_tree.h:626:32:   required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...) [with _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Key = int; _Val = std::pair<const int, PageTableEntry>; _KeyOfValue = std::_Select1st<std::pair<const int, PageTableEntry> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, PageTableEntry> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, PageTableEntry> >*]’
/usr/include/c++/7/bits/stl_tree.h:643:21:   required from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Key = int; _Val = std::pair<const int, PageTableEntry>; _KeyOfValue = std::_Select1st<std::pair<const int, PageTableEntry> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, PageTableEntry> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, PageTableEntry> >*]’
/usr/include/c++/7/bits/stl_tree.h:2398:33:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Key = int; _Val = std::pair<const int, PageTableEntry>; _KeyOfValue = std::_Select1st<std::pair<const int, PageTableEntry> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, PageTableEntry> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, PageTableEntry> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const int, PageTableEntry> >]’
/usr/include/c++/7/bits/stl_map.h:493:8:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = PageTableEntry; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, PageTableEntry> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = PageTableEntry; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’
Process.cpp:19:26:   required from here
/usr/include/c++/7/tuple:1652:70: error: no matching function for call to ‘PageTableEntry::PageTableEntry()’
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
In file included from ProcessManager.h:1:0,
                 from Process.cpp:3:
Process.h:80:2: note: candidate: PageTableEntry::PageTableEntry(int, int, int)
  PageTableEntry(int phyadd, int flagswap, int flagram)
  ^~~~~~~~~~~~~~
Process.h:80:2: note:   candidate expects 3 arguments, 0 provided
Process.h:76:16: note: candidate: constexpr PageTableEntry::PageTableEntry(const PageTableEntry&)
 typedef struct PageTableEntry 
                ^~~~~~~~~~~~~~
Process.h:76:16: note:   candidate expects 1 argument, 0 provided
Process.h:76:16: note: candidate: constexpr PageTableEntry::PageTableEntry(PageTableEntry&&)
Process.h:76:16: note:   candidate expects 1 argument, 0 provided

注意:此处发布的示例是大型项目的最少代码。我刚刚在这里发布了最小的可重现示例。

【问题讨论】:

这是主要原因error: no matching function for call to ‘PageTableEntry::PageTableEntry()’ PageTableEntry 不可默认构造,但[] 必须在缺少元素时默认构造该元素。使用findat 您确实需要提取minimal reproducible example 并将其与您的问题一起提供。很可能你会发现自己这样做的错误。另外,一旦代码运行,请考虑将代码放在 codereview.stackexchange.com 上,因为有些东西不是好的 C++。 如果你也能列出他的代码有问题的几点就好了。是不是没有遵循 c++ 标准,或者他做错了什么会从长远来看影响他 【参考方案1】:

我认为在您的 Process::readAddress 函数中您想打印一个 existing 条目。 因此,您需要确保在地图中找到所需的条目,而不是默默地为每个 virtualAddr 创建一个新条目 - 这就是 operator[] 将做的以及它需要的默认 ctor,正如 cmets 中提到的其他人.你可以使用std::map::find

int Process::readAddress(int virtualAddr, int lenToRead, char *buf)

    auto it = processmapram.find(virtualAddr);
    if(it != processmapram.end())
    
       cout << it->second.phyadd;
    

【讨论】:

以上是关于在 C++ 中访问 int 映射到类对象时出错的主要内容,如果未能解决你的问题,请参考以下文章

XML 文档中存在错误 (0, 0) - 将 XML 反序列化为对象时出错

将 JSON 映射到类对象

在 C++ 中转换类型时出错

尝试在 C++ 中使用 boost 正则表达式匹配从字符串转换为 int 时出错

在 Restkit 中映射单个 JSON 对象时出错

在 Restkit 中映射单个 JSON 对象时出错