【问题标题】:Templated 2D Array Error模板化二维数组错误
【发布时间】:2011-08-30 04:36:37
【问题描述】:

尊敬的先生, 当我阅读一本名为“Data Structures and Algorithms with Object-Oriented Design Patterns in C++”的在线书籍时,我从“二维数组实现”部分剪切并粘贴了一些代码 sn-ps(请参阅此链接以获取您的参考http://www.brpreiss.com/books/opus4/html/page102.html)如下:

#include "Array1D.h"

template <class T>
class CArray2D
{   
protected:
    unsigned int m_nRows;
    unsigned int m_nCols;
    CArray1D<T>  m_Array1D;

public:
    class Row
    {   
        CArray2D& m_Array2D;
        unsigned int const m_nRow;

    public:
        Row(CArray2D& Array2D, unsigned int nRow) : m_Array2D(Array2D), m_nRow(nRow) {}
        T& operator [] (unsigned int nCol) const { return m_Array2D.Select(m_nRow, nCol); }
    };

    CArray2D(unsigned int, unsigned int);
    T& Select(unsigned int, unsigned int);
    Row operator [] (unsigned int);
};

#include "StdAfx.h"
#include "Array2D.h"

template <class T>
CArray2D<T>::CArray2D(unsigned int nRows, unsigned int nCols)
            :m_nRows(nRows), 
             m_nCols(nCols), 
             m_Array1D(nRows * nCols)
{   
    // The constructor takes two arguments, nRows and nCols, which are the desired dimensions of the array. 
    // It calls the CArray1D<T> class constructor to build a one-dimensional array of size nRows * nCols.
}

template <class T>
T& CArray2D<T>::Select(unsigned int nRows, unsigned int nCols)
{   
    if (nRows >= m_nRows)
        throw std::out_of_range("invalid row");

    if (nCols >= m_nCols)
        throw std::out_of_range("invalid column");

    return m_Array1D[nRows * m_nCols + nCols];
}

template <class T>
CArray2D<T>::Row CArray2D<T>::operator [] (unsigned int nRow)
{   
    return Row(*this, nRow);
}

当我编译(Microsoft VS 2008 C++ 编译器)上述代码时,出现以下错误:

>Compiling...
1>Array2D.cpp
1>f:\tips\tips\array2d.cpp(27) : warning C4346: 'CArray2D<T>::Row' : dependent name is not a type
1>        prefix with 'typename' to indicate a type
1>f:\tips\tips\array2d.cpp(27) : error C2143: syntax error : missing ';' before 'CArray2D<T>::[]'
1>f:\tips\tips\array2d.cpp(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\tips\tips\array2d.cpp(27) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://f:\Tips\Tips\Debug\BuildLog.htm"
1>Tips - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

您能花点时间找出我的问题吗?

提前谢谢你。

金李

【问题讨论】:

  • 在谷歌搜索“依赖类型”C++,基本上CArray2D&lt;T&gt;::Row不能被编译器解析为类型,你必须使用typename关键字明确告诉编译器它是一个类型。搜索这个网站,有很多关于这个主题的问题和答案......

标签: c++ templates multidimensional-array


【解决方案1】:

Row 是依赖类型,因此您必须像这样添加typename

template <class T>
typename CArray2D<T>::Row CArray2D<T>::operator [] (unsigned int nRow)

有关 typename 和相关名称和类型的详细信息,请参阅 this question

【讨论】:

  • 亲爱的 kloffy 和 Georg Fritzsche:谢谢大家。
【解决方案2】:

你需要把 typename 放在这里:

template <class T>
typename CArray2D<T>::Row CArray2D<T>::operator [] (unsigned int nRow)
{   
    return Row(*this, nRow);
}

this question

【讨论】:

  • 亲爱的 kloffy,感谢您的解决方案。它就像一个魅力。
猜你喜欢
  • 2012-11-19
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 2017-12-17
  • 2017-11-16
  • 1970-01-01
相关资源
最近更新 更多