Ошибка при доступе к карте int для объектов класса в C ++ - PullRequest
0 голосов
/ 23 февраля 2020

У меня есть эта карта 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

Примечание: приведенный здесь пример является минимальным кодом для большой проект. Я только что выложил минимальный воспроизводимый пример здесь.

1 Ответ

1 голос
/ 23 февраля 2020

Я думаю, что в вашей функции Process::readAddress вы хотите напечатать существующую запись . Таким образом, вам нужно убедиться, что требуемая запись найдена на карте, а не молча создавать новую для каждого virtualAddr - это то, что будет делать operator[] и для чего ему нужен ctor по умолчанию, как другие упоминали в комментариях , Вы можете использовать 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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...