<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Template Problem]]></title><description><![CDATA[<p>Ich möchte eine Matrixklasse schreiben deren Werte beliebig gewählt werden können, d.h. einmal sollen die Einträge double sein können ein anderes mal zB int. Deshlab wollte ich eine Templateklasse schreiben. Soweit bin ich gekommen:</p>
<pre><code>#ifndef _MATRIX_H_
#define _MATRIX_H_	

template&lt;class T&gt;
class Matrix {

	public:
	T** A;
	unsigned int rows,cols;

	Matrix( unsigned int rows, unsigned int cols );
	~Matrix();

	double* sumMatrixCols();
	double* sumMatrixRows();
	double sumSumMatrix();
	void addRowVector( double* row, unsigned int length, unsigned int rowPos );
	void toString();

};

#endif
</code></pre>
<p>Und so sieht meine Klassenimplementation aus:</p>
<pre><code>#include &quot;Matrix.h&quot;
#include &lt;iostream&gt;

using namespace std;

Matrix::Matrix( unsigned int rows_, unsigned int cols_ ) {
	rows = rows_;
	cols = cols_;
	A = new T*[rows];
	for( int i = 0; i &lt; rows; ++i )
		A[i] = new T[cols];

}

Matrix::~Matrix() {
	for( int i = 0; i &lt; cols; ++i )
		delete A[i];
	delete A;
}

double* Matrix::sumMatrixCols() {
	double* sumVector = new double[rows];
	for( int i = 0; i &lt; rows; ++i )
		sumVector[i] = 0;

	for( int j = 0; j &lt; cols; ++j )
		for( int i = 0; i &lt; rows; ++i )
			sumVector[i] += this-&gt;A[i][j];

	return sumVector;
}

double* Matrix::sumMatrixRows() {
	double* sumVector = new double[cols];
	for( int j = 0; j &lt; cols; ++j )
		sumVector[j] = 0;

	for( int i = 0; i &lt; rows; ++i ) 
		for( int j = 0; j &lt; cols; ++j ) 

			sumVector[j] += this-&gt;A[i][j];

	return sumVector;
}

double Matrix::sumSumMatrix() {
	double sumSum = 0;
	for( int i = 0; i &lt; rows; ++i )
		for( int j = 0; j &lt; cols; ++j )
			sumSum += this-&gt;A[i][j];
	return sumSum;
}

void Matrix::addRowVector( double* row, unsigned int length, unsigned int rowPos ) {
	if( rows-1 &lt; rowPos )
		return;
	if( length &gt; cols )
		return;

	for( int j = 0; j &lt; cols; ++j ) 		
		this-&gt;A[rowPos][j] = row[j];
}

void Matrix::toString() {

	for( int i = 0; i &lt; rows; ++i ) {
		cout &lt;&lt; &quot;i = &quot; &lt;&lt; i &lt;&lt; &quot;\n&quot;;
		for( int j = 0; j &lt; cols; ++j )
			cout &lt;&lt; this-&gt;A[i][j] &lt;&lt; &quot;\t&quot;;
		cout &lt;&lt; &quot;\n&quot;;
	}
}
</code></pre>
<p>Leider erhalte ich beim Kompilieren folgenden Fehler:</p>
<blockquote>
<p>g++ -g -c Matrix.cpp<br />
Matrix.cpp:6: error: ‘template&lt;class T&gt; class Matrix’ used without template parameters<br />
Matrix.cpp:6: error: ISO C++ forbids declaration of ‘Matrix’ with no type<br />
Matrix.cpp: In function ‘int Matrix(unsigned int, unsigned int)’:<br />
Matrix.cpp:6: error: ‘int Matrix(unsigned int, unsigned int)’ redeclared as different kind of symbol<br />
Matrix.h:5: error: previous declaration of ‘template&lt;class T&gt; class Matrix’<br />
Matrix.cpp:7: error: ‘rows’ was not declared in this scope<br />
Matrix.cpp:8: error: ‘cols’ was not declared in this scope<br />
Matrix.cpp:9: error: ‘A’ was not declared in this scope</p>
</blockquote>
<p>Was bedeutet das, bzw was mache ich falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/266007/template-problem</link><generator>RSS for Node</generator><lastBuildDate>Thu, 03 Sep 2026 15:44:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/266007.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 02 May 2010 16:09:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Template Problem on Sun, 02 May 2010 16:09:52 GMT]]></title><description><![CDATA[<p>Ich möchte eine Matrixklasse schreiben deren Werte beliebig gewählt werden können, d.h. einmal sollen die Einträge double sein können ein anderes mal zB int. Deshlab wollte ich eine Templateklasse schreiben. Soweit bin ich gekommen:</p>
<pre><code>#ifndef _MATRIX_H_
#define _MATRIX_H_	

template&lt;class T&gt;
class Matrix {

	public:
	T** A;
	unsigned int rows,cols;

	Matrix( unsigned int rows, unsigned int cols );
	~Matrix();

	double* sumMatrixCols();
	double* sumMatrixRows();
	double sumSumMatrix();
	void addRowVector( double* row, unsigned int length, unsigned int rowPos );
	void toString();

};

#endif
</code></pre>
<p>Und so sieht meine Klassenimplementation aus:</p>
<pre><code>#include &quot;Matrix.h&quot;
#include &lt;iostream&gt;

using namespace std;

Matrix::Matrix( unsigned int rows_, unsigned int cols_ ) {
	rows = rows_;
	cols = cols_;
	A = new T*[rows];
	for( int i = 0; i &lt; rows; ++i )
		A[i] = new T[cols];

}

Matrix::~Matrix() {
	for( int i = 0; i &lt; cols; ++i )
		delete A[i];
	delete A;
}

double* Matrix::sumMatrixCols() {
	double* sumVector = new double[rows];
	for( int i = 0; i &lt; rows; ++i )
		sumVector[i] = 0;

	for( int j = 0; j &lt; cols; ++j )
		for( int i = 0; i &lt; rows; ++i )
			sumVector[i] += this-&gt;A[i][j];

	return sumVector;
}

double* Matrix::sumMatrixRows() {
	double* sumVector = new double[cols];
	for( int j = 0; j &lt; cols; ++j )
		sumVector[j] = 0;

	for( int i = 0; i &lt; rows; ++i ) 
		for( int j = 0; j &lt; cols; ++j ) 

			sumVector[j] += this-&gt;A[i][j];

	return sumVector;
}

double Matrix::sumSumMatrix() {
	double sumSum = 0;
	for( int i = 0; i &lt; rows; ++i )
		for( int j = 0; j &lt; cols; ++j )
			sumSum += this-&gt;A[i][j];
	return sumSum;
}

void Matrix::addRowVector( double* row, unsigned int length, unsigned int rowPos ) {
	if( rows-1 &lt; rowPos )
		return;
	if( length &gt; cols )
		return;

	for( int j = 0; j &lt; cols; ++j ) 		
		this-&gt;A[rowPos][j] = row[j];
}

void Matrix::toString() {

	for( int i = 0; i &lt; rows; ++i ) {
		cout &lt;&lt; &quot;i = &quot; &lt;&lt; i &lt;&lt; &quot;\n&quot;;
		for( int j = 0; j &lt; cols; ++j )
			cout &lt;&lt; this-&gt;A[i][j] &lt;&lt; &quot;\t&quot;;
		cout &lt;&lt; &quot;\n&quot;;
	}
}
</code></pre>
<p>Leider erhalte ich beim Kompilieren folgenden Fehler:</p>
<blockquote>
<p>g++ -g -c Matrix.cpp<br />
Matrix.cpp:6: error: ‘template&lt;class T&gt; class Matrix’ used without template parameters<br />
Matrix.cpp:6: error: ISO C++ forbids declaration of ‘Matrix’ with no type<br />
Matrix.cpp: In function ‘int Matrix(unsigned int, unsigned int)’:<br />
Matrix.cpp:6: error: ‘int Matrix(unsigned int, unsigned int)’ redeclared as different kind of symbol<br />
Matrix.h:5: error: previous declaration of ‘template&lt;class T&gt; class Matrix’<br />
Matrix.cpp:7: error: ‘rows’ was not declared in this scope<br />
Matrix.cpp:8: error: ‘cols’ was not declared in this scope<br />
Matrix.cpp:9: error: ‘A’ was not declared in this scope</p>
</blockquote>
<p>Was bedeutet das, bzw was mache ich falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891387</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891387</guid><dc:creator><![CDATA[HändyÄndy]]></dc:creator><pubDate>Sun, 02 May 2010 16:09:52 GMT</pubDate></item><item><title><![CDATA[Reply to Template Problem on Sun, 02 May 2010 16:14:46 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;typename T&gt;
Matrix&lt;T&gt;::Matrix( unsigned int rows, unsigned int cols )
{ 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1891392</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891392</guid><dc:creator><![CDATA[brotbernd]]></dc:creator><pubDate>Sun, 02 May 2010 16:14:46 GMT</pubDate></item><item><title><![CDATA[Reply to Template Problem on Sun, 02 May 2010 16:17:58 GMT]]></title><description><![CDATA[<p>Da das ja ein template ist, musst du dem Compiler das noch sagen:</p>
<pre><code class="language-cpp">template&lt;class T&gt;
Matrix&lt;T&gt;::Matrix( unsigned int rows_, unsigned int cols_ ) {
    rows = rows_;
    cols = cols_;
    A = new T*[rows];
    for( int i = 0; i &lt; rows; ++i )
        A[i] = new T[cols];

}
</code></pre>
<p>Und das bei allen anderen Funktionen ebenfalls.</p>
<p>Wenn du dich mit Spezialisierung beschäftigst wirst du sehen, dass der Compiler die Information da nicht einfach aus dem Hut herzaubern kann/soll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891393</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891393</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 02 May 2010 16:17:58 GMT</pubDate></item><item><title><![CDATA[Reply to Template Problem on Sun, 02 May 2010 16:26:13 GMT]]></title><description><![CDATA[<p>Die Summe von Ts ist übrigens auch wieder ein T.</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
class Matrix
{
public:
  typedef T value_type;

  value_type Sum() const
  {
    value_type sum = ...
       ...
    return sum;
  }
};
</code></pre>
<p>Das gleiche bei Methoden wie deiner addRow...<br />
bei einer Matrix&lt;int&gt; weist du dort den ints double Werte zu.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891399</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891399</guid><dc:creator><![CDATA[brotbernd]]></dc:creator><pubDate>Sun, 02 May 2010 16:26:13 GMT</pubDate></item><item><title><![CDATA[Reply to Template Problem on Sun, 02 May 2010 16:42:49 GMT]]></title><description><![CDATA[<p>Nich einen Kommentar zum Design: Du hast Dich mit <code>T** A</code> dazu entschlossen, Dich selbst um die Verwaltung zu kümmern. Das bedeutet, dass ein eigener Copy-Ctor, ein eigener Zuweisungsoperator und ein eigener Destruktor her muss. Letztes ist jedenfalls Pflicht. Die anderen könnte man auch privat deklarieren, um das Kopieren zu verhindern; denn sonst wird der Compiler welche automatisch generieren, die nicht das richtige tun.</p>
<p>Statdessen könnte man auch einfach folgendes machen:</p>
<pre><code class="language-cpp">class matrix
{
  int m, n;
  std::vector&lt;double&gt; coeffs;

  std::ptrdiff_t index4ij(int i, int j)
  { return static_cast&lt;std::ptrdiff_t&gt;(i)*n+j; } // row major order

public:
  matrix() : m(0), n(0) {}
  explicit matrix(int m, int n)
  : m(m), n(n), coeffs(std::ptrdiff_t(m)*n) {}

  double&amp; operator()(int i, int j) {return coeffs.at(index4ij(i,j));}

  ...
};
</code></pre>
<p>Oder:</p>
<pre><code class="language-cpp">class matrix
{
  int m, n;
  std::vector&lt;std::vector&lt;double&gt; &gt; coeffs;

public:
  matrix() : m(0), n(0) {}

  explicit matrix(int m, int n)
  : m(m), n(n), coeffs(m)
  {
    for (int i=0; i&lt;m; ++i)
      coeffs.at(i).resize(n);
  }

  double&amp; operator()(int i, int j) {return coeffs.at(i).at(j);}

  ...
};
</code></pre>
<p>In beiden Fällen kommst Du um das Definieren von eigenen Kopierkonstruktor, Zuweisungsoperator und Destruktor rum. Toll, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891406</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891406</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 02 May 2010 16:42:49 GMT</pubDate></item><item><title><![CDATA[Reply to Template Problem on Sun, 02 May 2010 16:46:26 GMT]]></title><description><![CDATA[<p>Noch etwas:</p>
<p>HändyÄndy schrieb:</p>
<blockquote>
<pre><code>#ifndef _MATRIX_H_
#define _MATRIX_H_
</code></pre>
</blockquote>
<p>Bezeichner, die mit _ anfangen oder zwei aufeinander folgende _ besitzen, sind reserviert und sollte nicht benutzt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891407</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891407</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 02 May 2010 16:46:26 GMT</pubDate></item><item><title><![CDATA[Reply to Template Problem on Sun, 02 May 2010 17:48:59 GMT]]></title><description><![CDATA[<p>Besten Dank für die Hinweise der Compiler meckert nun nicht mehr.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891439</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891439</guid><dc:creator><![CDATA[HändyÄndy]]></dc:creator><pubDate>Sun, 02 May 2010 17:48:59 GMT</pubDate></item></channel></rss>