c++ - Template Class : error C4430: missing type specifier - int assumed. 注意:C++不支持default-int

标签 c++ class templates header

我在代码中遇到了这个错误:

  • 错误 C2143:语法错误:缺少“;”在“<”
  • 之前
  • 查看对正在编译的类模板实例化“HashTable”的引用
  • 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++不支持default-int
  • 错误 C2238:“;”之前的意外标记

在私有(private)函数的这一行发现错误:

vector<HashEntry> array;

    #ifndef _HASHTABLE_H
    #define _HASHTABLE_H
    #include <vector>

    enum EntryType { ACTIVE, EMPTY, DELETED };

    Template <class HashedObj>
    struct HashEntry
    {
    HashedObj element;
    EntryType info;
    HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY )
    : element( e ), info( i ) { }
    };

    template <class HashedObj>
    class HashTable
    {

    private:
    vector<HashEntry> array;
    int currentSize;
    const HashedObj ITEM_NOT_FOUND;
    bool isActive( int currentPos ) const;
    int findPos( const HashedObj & x ) const;

    public: 
    explicit HashTable( const HashedObj & notFound, int size = 101 );
    HashTable( const HashTable & rhs )
    : ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ),
    array( rhs.array ), currentSize( rhs.currentSize ) { }


    const HashedObj & find( const HashedObj & x ) const;
    void makeEmpty( );
    void insert( const HashedObj & x );
    void remove( const HashedObj & x );
    const HashTable & operator=( const HashTable & rhs );

     };
    #endif

如何修复此错误 C4430?

最佳答案

HashEntry 本身是一个模板类,不会自动从包含的 HashTable 类中推断出模板参数类型。您需要按如下方式更改 array 声明

template <class HashedObj>
class HashTable {

private:
    std::vector<HashEntry<HashedObj>> array;
                      // ^^^^^^^^^^^
    // ...
};

关于c++ - Template Class : error C4430: missing type specifier - int assumed. 注意:C++不支持default-int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27213540/

相关文章:

jsp中的Java类

c++ - 在不知道类成员的情况下从基类实例创建派生类实例

c++ - 如何通过引用或指针将 `int` 和 `unsigned int` 成员传递给同一个函数?

c++ - MAKE 如何知道要检查哪个源文件

css - p.classname 或 .classname p,有什么区别吗?

c++ - 这是否可以减少免费模板函数的编译时间?

c++ - 是否可以出于性能目的创建默认初始化的类 std::is_trivial

c++ - "NULL pointer is passed"用于检测轮廓

c++ - 解析xml到对象,打印标签之间的内容-QT

java - 在 Eclipse 中创建新的 Java 项目不会创建(默认)包?