【问题标题】:Template Class: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int模板类:错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持默认整数
【发布时间】:2015-01-28 13:57:42
【问题描述】:

我在代码中有这个错误:

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

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

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 指定一个类型,因为您将其声明为模板:std::vector&lt;HashEntry&lt;HashedObj&gt;&gt; array;
  • _HASHTABLE_Hreserved name。您应该删除前导下划线。

标签: c++ class templates header


【解决方案1】:

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

template <class HashedObj>
class HashTable {

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

【讨论】:

  • 感谢您的回答。 std::vector> 数组;工作,但没有标准,它不工作。什么是 std:: ?
  • @bug std:: 指定vectorstd 命名空间中声明。您要么必须明确指定它,要么在使用该命名空间中的类之前放置 using namespace std;(尽管在大多数情况下不建议使用后者)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 2017-03-28
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多