Проблема в наследнике vector (STL)
От: izverg США  
Дата: 27.05.03 09:26
Оценка:
Здравствуйте.

Имеется следующий код (использую mingw32 (gcc 3.0.2)):

typedef unsigned char byte;

class Message : public std::vector<byte>
{
public:
    typedef void* dataT;
    typedef unsigned long sizeT;
    
    Message(){};
    Message(const Message& msg){ *this = msg; }
    virtual ~Message(){};
    
    void pop_size(int size)
    {
        if (this->size() >= (unsigned int)size)
         erase(begin(), begin()+size);
    }
    
    Message& add(const dataT p, sizeT s)
    {
        insert(end(), (byte*)p, (byte*)p+s);
        return *this;
    }
    
    Message& operator <<(int iv)
    {
        add(&iv,sizeof(int));
        return *this;
    }

    Message& operator <<(char c)
    {
        add(&c,sizeof(char));
        return *this;
    }
    
    Message& operator <<(const char* str)
    {
        int len = (int) strlen(str) + 1;
        operator <<(len);
        add((dataT)str,len);
        return *this;
    }
    
    Message& operator >>(string& str)
    {
        if (size() <= sizeof(int)) return *this;
        int len;
        operator >>(len);
        str = (char*)getdata();
        pop_size(len);
        return *this;
    }
    
    Message& operator >>(int& iv)
    {
        if ( size() < sizeof(int) ) return *this;
        iv = *(int*)getdata();
        pop_size(sizeof(int));
        return *this;
    }

    Message& operator >>(char& c)
    {
        if ( size() < sizeof(char) ) return *this;
        c = *(char*)getdata();
        pop_size(sizeof(char));
        return *this;
    }

    dataT getdata() const { return (dataT)begin(); }
};


При компиляции получаю ошибку в реализации метода getdata. Вот такую:

mmm.cpp: In member function `void* Message::getdata() const':
mmm.cpp:77: cannot convert
`std::vector<_Tp, _Alloc>::begin() const [with _Tp = byte, _Alloc = std::allocator<byte>]()'

from type

`__gnu_cxx::__normal_iterator<const byte*, std::vector<byte, std::allocator<byte> > >' to type `void*'


Как мне в getdata получить константный void* на первый элемент вектора (подчистку вектора делаю методом pop_size) ?

Заранее спасибо. Буду благодарен если кто-либо напишет мне в аську на 53898432/
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.