lear
lear

Reputation: 25

how to get the map type pointer in c++, map

I want to write something to share memory, pAttr is the share memory address.

The template function as below, but it does not pass the compile.

template <typename Container>
int ShareMemMgn::writeContainerToShareMemMap(void* pAttr, Container& oData)
{
    typename Container::mapped_type T;
    (T*)(pElem) = (T *)(pAttr); //compile errror
/*
share_mem_mgn.cpp:545: error: expected primary-expression before ‘)’ token
share_mem_mgn.cpp:545: error: ‘pElem’ was not declared in this scope
share_mem_mgn.cpp:545: error: expected primary-expression before ‘)’ token

*/


    for(typename Container::iterator it = oData.begin();
        it != oData.end(); ++it)
    {
        memcpy(pElem, (&(it->second)), sizeof(typename Container::mapped_type));
        ++pElem;
    }

    return 0;
}

How to get the maped type pointer? Could anyone help me? Thanks a lot.

Upvotes: 0

Views: 136

Answers (2)

Karthik T
Karthik T

Reputation: 31972

You could also do this

template <typename KeyType, typename ValueType>
int ShareMemMgn::writeContainerToShareMemMap(void* pAttr, std::map<KeyType,ValueType>& oData)

If you are using only a Map.

Upvotes: 1

cdhowie
cdhowie

Reputation: 169478

As your code reads right now, T is a variable, not a type. Presumably you meant this:

typedef typename Container::mapped_type T;
T * pElem = static_cast<T *>(pAttr);

Upvotes: 3

Related Questions