Note that there are some explanatory texts on larger screens.

plurals
  1. POTemplated 2D Array Error
    primarykey
    data
    text
    <p>Dear Sir, When I read an on-line book titled "Data Structures and Algorithms with Object-Oriented Design Patterns in C++", I cut and paste some code snippets from the section "Two-Dimensional Array Implementation" (please see this link for your reference <a href="http://www.brpreiss.com/books/opus4/html/page102.html" rel="nofollow">http://www.brpreiss.com/books/opus4/html/page102.html</a>) as follows:</p> <pre><code>#include "Array1D.h" template &lt;class T&gt; class CArray2D { protected: unsigned int m_nRows; unsigned int m_nCols; CArray1D&lt;T&gt; m_Array1D; public: class Row { CArray2D&amp; m_Array2D; unsigned int const m_nRow; public: Row(CArray2D&amp; Array2D, unsigned int nRow) : m_Array2D(Array2D), m_nRow(nRow) {} T&amp; operator [] (unsigned int nCol) const { return m_Array2D.Select(m_nRow, nCol); } }; CArray2D(unsigned int, unsigned int); T&amp; Select(unsigned int, unsigned int); Row operator [] (unsigned int); }; </code></pre> <hr> <pre><code>#include "StdAfx.h" #include "Array2D.h" template &lt;class T&gt; CArray2D&lt;T&gt;::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&lt;T&gt; class constructor to build a one-dimensional array of size nRows * nCols. } template &lt;class T&gt; T&amp; CArray2D&lt;T&gt;::Select(unsigned int nRows, unsigned int nCols) { if (nRows &gt;= m_nRows) throw std::out_of_range("invalid row"); if (nCols &gt;= m_nCols) throw std::out_of_range("invalid column"); return m_Array1D[nRows * m_nCols + nCols]; } template &lt;class T&gt; CArray2D&lt;T&gt;::Row CArray2D&lt;T&gt;::operator [] (unsigned int nRow) { return Row(*this, nRow); } </code></pre> <p>When I compile (Microsoft VS 2008 C++ compiler) the above code, I got the following errors:</p> <pre><code>&gt;Compiling... 1&gt;Array2D.cpp 1&gt;f:\tips\tips\array2d.cpp(27) : warning C4346: 'CArray2D&lt;T&gt;::Row' : dependent name is not a type 1&gt; prefix with 'typename' to indicate a type 1&gt;f:\tips\tips\array2d.cpp(27) : error C2143: syntax error : missing ';' before 'CArray2D&lt;T&gt;::[]' 1&gt;f:\tips\tips\array2d.cpp(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1&gt;f:\tips\tips\array2d.cpp(27) : fatal error C1903: unable to recover from previous error(s); stopping compilation 1&gt;Build log was saved at "file://f:\Tips\Tips\Debug\BuildLog.htm" 1&gt;Tips - 3 error(s), 1 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== </code></pre> <p>Could you take some time to figure out my problems? </p> <p>Thank you in advance.</p> <p>Golden Lee</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload