【问题标题】:For a template: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int对于模板:错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持默认整数
【发布时间】:2012-03-21 08:27:11
【问题描述】:
#include <list>

template < class TYPE > 
class CIndex : protected std::list < TYPE >
{
public:
    typedef std::list < TYPE >::iterator  CIndexIt;
    typedef std::list < TYPE >::difference_type  CIndexDiff;

错误发生在上述代码的最后一行。

我见过thisthe msdn page,但都不能解决我的错误。

任何人都知道可能导致问题的原因吗?

编辑:
第一个链接的解决方案不起作用的原因是,虽然添加 typename 对上述代码有效,但对以下代码无效:

#include<hash_map>
class CWItems
{
typedef stdext::hash_map < unsigned long, CWksItem* >   CItem;
CItem mItems;

所以我认为我在到处添加typename 做错了事。在此代码中的typedef 之后使用typename 会导致此错误:

error C2899: typename cannot be used outside a template declaration

没有typename,显示的错误是error C4430: missing type specifier - int assumed. Note: C++ does not support default-int,在CItem mItems; 行。

【问题讨论】:

  • 对您发布的链接很有用。我的问题仍然没有解决。已编辑我的问题以添加更多信息。帮忙?
  • CWItems 不是类模板,因此您不能有任何依赖名称。 CWksItem 声明在哪里?
  • CWksItem 被称为class CWksItem;(前向声明)。但即使将 CWksItem 替换为 int,错误仍然存​​在。
  • 我认为问题出在其他地方。我为 hash_map 写了一些独立的代码并且它工作。谢谢。

标签: c++


【解决方案1】:

您需要添加typename 关键字,因为std::list&lt;TYPE&gt;::iteratorstd::list&lt;TYPE&gt;::difference_type 是从属名称:

typedef typename std::list < TYPE >::iterator  CIndexIt;
typedef typename std::list < TYPE >::difference_type  CIndexDiff;

更多信息请参见http://pages.cs.wisc.edu/~driscoll/typename.html

【讨论】:

  • 问题出现在代码的另一部分,这次typename没有解决。我在问题的编辑部分中提到了更多。帮忙?
【解决方案2】:

您在链接到的问题中缺少 typename 关键字:

typedef typename std::list < TYPE >::iterator  CIndexIt;
typedef typename std::list < TYPE >::difference_type  CIndexDiff;

【讨论】:

    猜你喜欢
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 2017-03-28
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多