简体   繁体   English

C ++期望的类型说明符

[英]C++ expected type-specifier

Background 背景

I am in the middle of a project writing stl-like containers for the Arduino. 我正处于为Arduino编写类似stl的容器的项目中 So far I have successfully written deque, vector, and string. 到目前为止,我已成功编写了deque,vector和string。

Problem 问题

I have run into a problem with the map container. 我遇到了map容器的问题。 For some reason, in the map::insert() method, the compiler is telling me that it is expecting a type specifier. 出于某种原因,在map::insert()方法中,编译器告诉我它期待一个类型说明符。 I have included all the code related to the problem. 我已经包含了与问题相关的所有代码。 Any help would be appreciated. 任何帮助,将不胜感激。

///////////////////////////////////////////////////////////////////////////////
// pair
///////////////////////////////////////////////////////////////////////////////
template<typename K, typename V>
class pair {
public:
    pair( const K& key, const V& val )
    : _key_( key )
    , _val_( val )
    {}

    pair( const pair& p )
    : _key_( p.key() )
    , _val_( p.val() )
    {}

    virtual ~pair(){}

    K& key(){
        return _key_;
    }

    V& val(){
        return _val_;
    }

private:
    K _key_;
    V _val_;
};

///////////////////////////////////////////////////////////////////////////////
// map
///////////////////////////////////////////////////////////////////////////////
template<typename K, typename V>
class map {
public:
    map()
    : _size_( 0 )
    , _items_( 0 )
    {}

    virtual ~map(){
        for( int i = 0; i < _size_; ++i )
            delete _items_[i];

        free( _items_ );

    }

    void insert( const pair<K,V>& p ){
        _items_ = reinterpret_cast<kick::pair<K,V>**>( realloc( _items_, (sizeof( void* ) * (++_size_)) ) );
        _items_[_size_ - 1] = new pair( p ); //error: expected type-specifier
    }

    V& operator[]( const K& key ){
        for( int i = 0; i < _size_; ++i ){
            if( _items_[i].key() == key )
                return _items_[i].val();

        }

    }

private:
    int _size_;
    pair<K,V>** _items_;

};

pair is just a template, not a type. pair只是一个模板,而不是一个类型。 The compiler is expecting the template parameters, which in this case are types. 编译器期望模板参数,在这种情况下是类型。 This is, it is expecting your line to be: 这是,它期待您的行:

_items_[_size_ - 1] = new pair<K,V>( p );

No, it cannot deduce the template parameters; 不,它不能推断出模板参数; this only works for template functions, not types. 这仅适用于模板功能,而不适用于类型。

new pair<K,V>( p ) is what it wants there. new pair<K,V>( p )就是它想要的东西。

For myself, I'd implement a flat-map on top of vector so I didn't have to do memory management in it. 对于我自己,我会在vector上实现平面地图,所以我不必在其中进行内存管理。 An advantage of this is that you can remove a level of indirection. 这样做的一个优点是您可以删除一个间接级别。 (Indirection is the performance killer). (间接是性能杀手)。

Another thought is to have a high-water sorting mark, where things before the sorting mark are sorted, and things after it are jumbled. 另一个想法是有一个高水分类标记,排序标记之前的东西被排序,之后的东西是混乱的。 This can give you fewer comparisons when the container gets large. 当容器变大时,这可以减少对比。 But first get your flat-map working. 但首先让你的平面地图工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM