<?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[Class1 cannot access private member declared in Class1]]></title><description><![CDATA[<p>Also ich habe mir eine Punkt-Klasse (mit variabler Dimension) gebastelt - und jetzt ein (mir unerklärliches) Problem:</p>
<pre><code class="language-cpp">template &lt;size_t Tct&gt;
class TPoint
{
public:
	static const size_t dimension = Tct;
private:
	float value[dimension];

	template &lt;size_t Tct_rhs&gt;
	void swap (float _value[Tct_rhs])
	{
		if(dimension &lt;= Tct_rhs)
		{
			std::copy (_value, _value+dimension, value);
		}
		else	//Tct_rhs &lt; dim
		{
			std::copy (_value, _value+Tct_rhs, value);
			for(float *i(value+Tct_rhs+1), *e(value+dimension); i != e; ++i)
			{
				*i = 0;
			}
		}
	}
public:
	float&amp; operator[] (size_t i)
	{
		assert (i &lt; dimension);
		return value[i];
	}

	const float&amp; operator[] (size_t i) const
	{
		assert (i &lt; dimension);
		return value[i];
	}

//standard-Konstruktoren etc

	template &lt;size_t Tct_rhs&gt;
	TPoint (const TPoint&lt;Tct_rhs&gt; &amp;rhs)
	{
		swap (rhs.value);
	}

	template &lt;size_t Tct_rhs&gt;
	TPoint&amp; operator = (const TPoint&lt;Tct_rhs&gt; &amp;rhs)
	{
		swap(rhs.value);
		return *this;
	}

//Operatoren etc
};
</code></pre>
<p>Die Fehlermeldung noch mal:<br />
(wie man schon sieht arbeite ich mit VS (2008) )<br />
Nur verstehe ich nicht, wie die selbe Klasse auf einmal nicht mit den privaten Membern arbeiten darf - nur weil sie nen anderes Template-Argument hat - oder hab ich irgendwo anders nen Fehler gemacht und nur zu blind, den zu erkennen?</p>
<pre><code>1&gt;f:\c\libs\math\point.hpp(69) : error C2248: 'my::TPoint&lt;Tct&gt;::value' : cannot access private member declared in class 'my::TPoint&lt;Tct&gt;'
1&gt;        with
1&gt;        [
1&gt;            Tct=3
1&gt;        ]
1&gt;        f:\c\libs\math\point.hpp(21) : see declaration of 'my::TPoint&lt;Tct&gt;::value'
1&gt;        with
1&gt;        [
1&gt;            Tct=3
1&gt;        ]
1&gt;        c:\users\tommy\documents\visual studio 2008\projects\test\test\main.cpp(51) : see reference to function template instantiation 'my::TPoint&lt;Tct&gt;::TPoint&lt;3&gt;(const my::TPoint&lt;3&gt; &amp;)' being compiled
1&gt;        with
1&gt;        [
1&gt;            Tct=2
1&gt;        ]
1&gt;f:\c\libs\math\point.hpp(69) : error C2784: 'void my::TPoint&lt;Tct&gt;::swap(float [Tct_rhs])' : could not deduce template argument for 'float [Tct_rhs]' from 'const float [3]'
1&gt;        with
1&gt;        [
1&gt;            Tct=2
1&gt;        ]
1&gt;        f:\c\libs\math\point.hpp(24) : see declaration of 'my::TPoint&lt;Tct&gt;::swap'
1&gt;        with
1&gt;        [
1&gt;            Tct=2
1&gt;        ]
1&gt;f:\c\libs\math\point.hpp(69) : error C2248: 'my::TPoint&lt;Tct&gt;::value' : cannot access private member declared in class 'my::TPoint&lt;Tct&gt;'
1&gt;        with
1&gt;        [
1&gt;            Tct=3
1&gt;        ]
1&gt;        f:\c\libs\math\point.hpp(21) : see declaration of 'my::TPoint&lt;Tct&gt;::value'
1&gt;        with
1&gt;        [
1&gt;            Tct=3
1&gt;        ]
1&gt;        c:\users\tommy\documents\visual studio 2008\projects\test\test\main.cpp(52) : see reference to function template instantiation 'my::TPoint&lt;Tct&gt;::TPoint&lt;3&gt;(const my::TPoint&lt;3&gt; &amp;)' being compiled
1&gt;        with
1&gt;        [
1&gt;            Tct=4
1&gt;        ]
1&gt;f:\c\libs\math\point.hpp(69) : error C2784: 'void my::TPoint&lt;Tct&gt;::swap(float [Tct_rhs])' : could not deduce template argument for 'float [Tct_rhs]' from 'const float [3]'
1&gt;        with
1&gt;        [
1&gt;            Tct=4
1&gt;        ]
1&gt;        f:\c\libs\math\point.hpp(24) : see declaration of 'my::TPoint&lt;Tct&gt;::swap'
1&gt;        with
1&gt;        [
1&gt;            Tct=4
1&gt;        ]
</code></pre>
<p>Der passende Code zur Anwendung:</p>
<pre><code class="language-cpp">TPoint &lt;3&gt; a;
  a[0] = 1;
  a[1] = 2;
  a[3] = 3;
TPoint &lt;2&gt; b(a);
TPoint &lt;4&gt; c(a);
</code></pre>
<p>Den Fehler verstehe ich aber nicht - oder müsste ich da noch irgendwo irgendwie irgendetwas mit friend versuchen? Das würde aber nur den ersten und dritten Fehler erklären - nicht den zweiten und vierten. Und wie hättet ihr das gelöst (mal davon abgesehen davon, dass man auch fertige Libs/... verwenden könnte - möchte ich aber nicht ^^)?</p>
<p>Danke schon mal und bb</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/229430/class1-cannot-access-private-member-declared-in-class1</link><generator>RSS for Node</generator><lastBuildDate>Sun, 27 Sep 2026 18:40:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/229430.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 11 Dec 2008 16:52:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Thu, 11 Dec 2008 16:52:34 GMT]]></title><description><![CDATA[<p>Also ich habe mir eine Punkt-Klasse (mit variabler Dimension) gebastelt - und jetzt ein (mir unerklärliches) Problem:</p>
<pre><code class="language-cpp">template &lt;size_t Tct&gt;
class TPoint
{
public:
	static const size_t dimension = Tct;
private:
	float value[dimension];

	template &lt;size_t Tct_rhs&gt;
	void swap (float _value[Tct_rhs])
	{
		if(dimension &lt;= Tct_rhs)
		{
			std::copy (_value, _value+dimension, value);
		}
		else	//Tct_rhs &lt; dim
		{
			std::copy (_value, _value+Tct_rhs, value);
			for(float *i(value+Tct_rhs+1), *e(value+dimension); i != e; ++i)
			{
				*i = 0;
			}
		}
	}
public:
	float&amp; operator[] (size_t i)
	{
		assert (i &lt; dimension);
		return value[i];
	}

	const float&amp; operator[] (size_t i) const
	{
		assert (i &lt; dimension);
		return value[i];
	}

//standard-Konstruktoren etc

	template &lt;size_t Tct_rhs&gt;
	TPoint (const TPoint&lt;Tct_rhs&gt; &amp;rhs)
	{
		swap (rhs.value);
	}

	template &lt;size_t Tct_rhs&gt;
	TPoint&amp; operator = (const TPoint&lt;Tct_rhs&gt; &amp;rhs)
	{
		swap(rhs.value);
		return *this;
	}

//Operatoren etc
};
</code></pre>
<p>Die Fehlermeldung noch mal:<br />
(wie man schon sieht arbeite ich mit VS (2008) )<br />
Nur verstehe ich nicht, wie die selbe Klasse auf einmal nicht mit den privaten Membern arbeiten darf - nur weil sie nen anderes Template-Argument hat - oder hab ich irgendwo anders nen Fehler gemacht und nur zu blind, den zu erkennen?</p>
<pre><code>1&gt;f:\c\libs\math\point.hpp(69) : error C2248: 'my::TPoint&lt;Tct&gt;::value' : cannot access private member declared in class 'my::TPoint&lt;Tct&gt;'
1&gt;        with
1&gt;        [
1&gt;            Tct=3
1&gt;        ]
1&gt;        f:\c\libs\math\point.hpp(21) : see declaration of 'my::TPoint&lt;Tct&gt;::value'
1&gt;        with
1&gt;        [
1&gt;            Tct=3
1&gt;        ]
1&gt;        c:\users\tommy\documents\visual studio 2008\projects\test\test\main.cpp(51) : see reference to function template instantiation 'my::TPoint&lt;Tct&gt;::TPoint&lt;3&gt;(const my::TPoint&lt;3&gt; &amp;)' being compiled
1&gt;        with
1&gt;        [
1&gt;            Tct=2
1&gt;        ]
1&gt;f:\c\libs\math\point.hpp(69) : error C2784: 'void my::TPoint&lt;Tct&gt;::swap(float [Tct_rhs])' : could not deduce template argument for 'float [Tct_rhs]' from 'const float [3]'
1&gt;        with
1&gt;        [
1&gt;            Tct=2
1&gt;        ]
1&gt;        f:\c\libs\math\point.hpp(24) : see declaration of 'my::TPoint&lt;Tct&gt;::swap'
1&gt;        with
1&gt;        [
1&gt;            Tct=2
1&gt;        ]
1&gt;f:\c\libs\math\point.hpp(69) : error C2248: 'my::TPoint&lt;Tct&gt;::value' : cannot access private member declared in class 'my::TPoint&lt;Tct&gt;'
1&gt;        with
1&gt;        [
1&gt;            Tct=3
1&gt;        ]
1&gt;        f:\c\libs\math\point.hpp(21) : see declaration of 'my::TPoint&lt;Tct&gt;::value'
1&gt;        with
1&gt;        [
1&gt;            Tct=3
1&gt;        ]
1&gt;        c:\users\tommy\documents\visual studio 2008\projects\test\test\main.cpp(52) : see reference to function template instantiation 'my::TPoint&lt;Tct&gt;::TPoint&lt;3&gt;(const my::TPoint&lt;3&gt; &amp;)' being compiled
1&gt;        with
1&gt;        [
1&gt;            Tct=4
1&gt;        ]
1&gt;f:\c\libs\math\point.hpp(69) : error C2784: 'void my::TPoint&lt;Tct&gt;::swap(float [Tct_rhs])' : could not deduce template argument for 'float [Tct_rhs]' from 'const float [3]'
1&gt;        with
1&gt;        [
1&gt;            Tct=4
1&gt;        ]
1&gt;        f:\c\libs\math\point.hpp(24) : see declaration of 'my::TPoint&lt;Tct&gt;::swap'
1&gt;        with
1&gt;        [
1&gt;            Tct=4
1&gt;        ]
</code></pre>
<p>Der passende Code zur Anwendung:</p>
<pre><code class="language-cpp">TPoint &lt;3&gt; a;
  a[0] = 1;
  a[1] = 2;
  a[3] = 3;
TPoint &lt;2&gt; b(a);
TPoint &lt;4&gt; c(a);
</code></pre>
<p>Den Fehler verstehe ich aber nicht - oder müsste ich da noch irgendwo irgendwie irgendetwas mit friend versuchen? Das würde aber nur den ersten und dritten Fehler erklären - nicht den zweiten und vierten. Und wie hättet ihr das gelöst (mal davon abgesehen davon, dass man auch fertige Libs/... verwenden könnte - möchte ich aber nicht ^^)?</p>
<p>Danke schon mal und bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1628973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1628973</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 11 Dec 2008 16:52:34 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Thu, 11 Dec 2008 19:35:06 GMT]]></title><description><![CDATA[<p><a href="https://www.google.de/search?&amp;q=c%2B%2B%20index%20mod" rel="nofollow">Google: c++ index mod</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629095</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629095</guid><dc:creator><![CDATA[mmxl]]></dc:creator><pubDate>Thu, 11 Dec 2008 19:35:06 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Thu, 11 Dec 2008 20:29:11 GMT]]></title><description><![CDATA[<p>mmxl schrieb:</p>
<blockquote>
<p><a href="https://www.google.de/search?&amp;q=c%2B%2B%20index%20mod" rel="nofollow">Google: c++ index mod</a></p>
</blockquote>
<p>Danke aber den Ratschlag versteh ich nicht - ja, ich hab gesehen, dass es ein Link ist und hab auch draufgeklickt und gelesen - nur leider keinen Zusammenhang zwischen den (ziemlich wenigen) Ergebnissen und meinem Problem gefunden -.-</p>
<p>bb</p>
<p>edit:<br />
Ok - der Fehler daran ist, dass:</p>
<pre><code class="language-cpp">template &lt;size_t Tct_rhs&gt;
TPoint (const TPoint&lt;Tct_rhs&gt; &amp;rhs) //er erkennt TPoint&lt;Tct_rhs&gt; nicht so, wie ich es meine - hat niemand eine Idee, wie ich das anstellen kann, dass es ein Copy-CTor für eine Instanz mit einem anderen Template-Argument gibt?
{
	swap (rhs.value);
}
</code></pre>
<p>Danke schon mal... bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629103</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629103</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 11 Dec 2008 20:29:11 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Thu, 11 Dec 2008 21:32:33 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<pre><code class="language-cpp">template &lt;size_t Tct_rhs&gt;
TPoint (const TPoint&lt;Tct_rhs&gt; &amp;rhs) //er erkennt TPoint&lt;Tct_rhs&gt; nicht so, wie ich es meine
</code></pre>
</blockquote>
<p>Hast du denn eine Erklärung gefunden, warum das so nicht geht? Würde mich mal interessieren, denn ich hätte das wahrscheinlich genauso geschrieben, und wäre über das selbe Problem gestolpert. Wie es &quot;richtig&quot; wäre kann ich dir aber leider auch nicht sagen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629153</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629153</guid><dc:creator><![CDATA[dooooomi]]></dc:creator><pubDate>Thu, 11 Dec 2008 21:32:33 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Thu, 11 Dec 2008 21:50:11 GMT]]></title><description><![CDATA[<p>Nein, imho geht es einfach nicht die noch nicht fertige Klasse in sich selber zu verwenden - denke, ich werde das jetzt über eine freie Funktion lösen...</p>
<p>Aber naja - mal gucken, inwiefern das so geht, das überhaupt geht usw ^^ Ich meld mich vrmtl noch mal, wenn ich weiß, wie ich es gelöst habe ^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629158</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629158</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 11 Dec 2008 21:50:11 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Thu, 11 Dec 2008 23:07:31 GMT]]></title><description><![CDATA[<p>ok - ich habs jz doch endlich mal geschafft:</p>
<pre><code class="language-cpp">template &lt;size_t Tct&gt;
class TPoint
{
	template &lt;size_t lhs_count, size_t rhs_count&gt;
	friend
		TPoint &lt;lhs_count&gt; Point (const TPoint &lt;rhs_count&gt; &amp;rhs);

public:
	static const size_t dimension = Tct;
private:
	float value[dimension];
	template &lt;size_t Tct_rhs&gt;
	void copy (const float *first)
	{
		if(dimension &lt;= Tct_rhs)
		{
			std::copy (first, first+dimension, value);
		}
		else	//Tct_rhs &lt; dim
		{
			std::copy (first, first+Tct_rhs, value);
			for(float *i(value+Tct_rhs+1), *e(value+dimension); i != e; ++i)
			{
				*i = 0;
			}
		}
	}
public:
	float&amp; operator[] (size_t i)
	{
		assert (i &lt; dimension);
		return value[i];
	}

	const float&amp; operator[] (size_t i) const
	{
		assert (i &lt; dimension);
		return value[i];
	}
	TPoint()
	{
		for (float *iter(value), *end(value+dimension); iter != end; ++iter)
		{
			*iter = 0;
		}
	}

	template &lt;size_t Tct_rhs&gt;
	TPoint(float _value[Tct_rhs])
	{
		copy (_value);
	}

	TPoint (const TPoint &amp;rhs)
	{
		copy &lt;rhs.dimension&gt; (rhs.value);
	}

	TPoint&amp; operator = (const TPoint &amp;rhs)
	{
		copy &lt;rhs.dimension&gt; (rhs.value);
		return *this;
	}
};

template &lt;size_t lhs_count, size_t rhs_count&gt;
TPoint &lt;lhs_count&gt; Point (const TPoint &lt;rhs_count&gt; &amp;rhs)
{
	TPoint &lt;lhs_count&gt; lhs;
	lhs.copy &lt;rhs_count&gt; (rhs.value);
	return lhs;
}
</code></pre>
<p>Es gefällt mir zwar nun nicht gerade perfekt, aber es funktioniert ^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629189</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629189</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 11 Dec 2008 23:07:31 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Thu, 11 Dec 2008 23:14:18 GMT]]></title><description><![CDATA[<p><a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-202817-and-highlight-is-.html" rel="nofollow">Ich hatte das Problem auch mal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629191</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629191</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Thu, 11 Dec 2008 23:14:18 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Thu, 11 Dec 2008 23:28:21 GMT]]></title><description><![CDATA[<p>ahh ^^ danke<br />
hmmm... nach friend hatte ich zwar gefragt aber hätte nicht gedacht, dass das notwendig sei...</p>
<p>Ich denke aber trotzdem nicht, dass das geht, was ich am Anfang versucht hatte - ist vielleicht auch gar nicht so gut, wenn der User explizit sagen muss, dass sich die Anzahl der Dimensionen ändern soll...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629196</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629196</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 11 Dec 2008 23:28:21 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Fri, 12 Dec 2008 09:47:50 GMT]]></title><description><![CDATA[<p>Das ist denke ich einer der Standard-Denkfehler wenns um Templateklassen geht: zwei verschiedene Instantiierungen eines Klassentemplates sind auch zwei verschiedene Klassen und können nicht auf die privates der anderen Instantiierungen zugreifen. Auch wenn der Name des Templates der selbe ist.</p>
<p>Dennoch könnte in deinem Fall der Pseudo-Copy-Ctor ohne friends funktionieren:</p>
<pre><code class="language-cpp">class DaddyPoint
{
proteted:
  virtual void copy(std::size_t n, float* target) = 0;
};

template &lt;size_t Tct&gt;
class TPoint : public DaddyPoint
{
public:
  static const size_t dimension = Tct;
private:
  float value[dimension];
public:
  template &lt;size_t Tct2&gt;
  explicit TPoint(TPoint&lt;Tct2&gt; const&amp; other)
  {
    DaddyPoint const&amp; othersDaddy = other;
    othersDaddy.copy(dimension, value);
  }
private:
  virtual void copy(size_t n, float* target)
  {
    std::copy(value, vlaue + std::min(n, dimension), target);
    if (dimension &lt; n)
    {
      std::fill(target+dimension, target+n, 0.0f);
    }
  }
};
</code></pre>
<p>Der Trick ist, dass man dem &quot;fremden&quot; Punkt den Zugriff gewährt und sagt &quot;mach mal bitte&quot;, statt selber auf dessen innereien zuzugreifen. Das explicit beim Pseudo-Copy (Pseudo, weils ja von ner anderen Dimension ist) halte ich für notwendig, sonst würde folgendes ohne weitere Warnungen compilieren:</p>
<pre><code class="language-cpp">TPoint&lt;1&gt; p1;
TPoint&lt;2&gt; p2;
TPoint&lt;3&gt; p3;

p2 = p1 + p3; //vorausgesetzt es gibt den op+ als Member von TPoint
</code></pre>
<p>die letzte Zeile ist etwas tricky: Wenn TPoint einen op+(TPoint) als Member hat, dann wird p3 stillschweigend umgewandelt in einen TPoint&lt;1&gt;, daraufhin wird das Ganze beim op= implizit in einen TPoint&lt;2&gt; gewandelt. Um sowas zu vermeiden ist explicit ja da <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Ich hab extra auf op+ als Member angesprochen, denn wenns einen freien Operator+ mit zwei gleichen TPoint-argumenten gibt (natürlich ein template), dann kann der Compiler nicht entscheiden welchen der Parameter er an den anderen anpassen soll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629315</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629315</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 12 Dec 2008 09:47:50 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Fri, 12 Dec 2008 13:19:56 GMT]]></title><description><![CDATA[<p>hmm... Danke - ich habe es jetzt über eine extra Funktion geregelt und finde das im Nachhinein auch schöner (eben weil die Umwandlungen nicht &quot;von Geisterhand&quot; passieren können ^^)</p>
<p>Falls es jemanden interessiert:</p>
<pre><code class="language-cpp">template &lt;size_t Tct&gt;
class TPoint
{
	template &lt;size_t lhs_count, size_t rhs_count&gt;
	friend
		TPoint &lt;lhs_count&gt; Point (const TPoint &lt;rhs_count&gt; &amp;rhs);

public:
	static const size_t dimension = Tct;
private:
	float value[dimension];

	template &lt;size_t Tct_rhs&gt;
	void copy (const float *first)
	{
		if(dimension &lt;= Tct_rhs)
		{
			std::copy (first, first+dimension, value);
		}
		else	//Tct_rhs &lt; dim
		{
			std::copy (first, first+Tct_rhs, value);
			for(float *i(value+Tct_rhs+1), *e(value+dimension); i != e; ++i)
			{
				*i = 0;
			}
		}
	}
public:
	float&amp; operator[] (size_t i)
	{
		assert (i &lt; dimension);
		return value[i];
	}

	const float&amp; operator[] (size_t i) const
	{
		assert (i &lt; dimension);
		return value[i];
	}

	TPoint()
	{
		for (float *iter(value), *end(value+dimension); iter != end; ++iter)
		{
			*iter = 0;
		}
	}

	template &lt;size_t Tct_rhs&gt;
	TPoint(float _value[Tct_rhs])
	{
		copy (_value);
	}

	TPoint (const TPoint &amp;rhs)
	{
		copy &lt;rhs.dimension&gt; (rhs.value);
	}

	TPoint&amp; operator = (const TPoint &amp;rhs)
	{
		copy &lt;rhs.dimension&gt; (rhs.value);
		return *this;
	}

	TPoint&amp; operator += (const TPoint &amp;rhs)
	{
		const float *from (rhs.value);
		for (float *iter (value), *end (value+dimension); iter != end; ++iter, ++from)
		{
			*iter += *from;
		}
		return *this;
	}

	TPoint operator - () const
	{
		TPoint tmp(*this);
		for (float *iter(tmp.value), *end(tmp.value+dimension); iter != end; ++iter)
		{
			*iter = -*iter;
		}
		return tmp;
	}

	TPoint&amp; operator -= (const TPoint &amp;rhs)
	{
		return *this += -rhs;
	}

};

template &lt;size_t lhs_count, size_t rhs_count&gt;
TPoint &lt;lhs_count&gt; Point (const TPoint &lt;rhs_count&gt; &amp;rhs)
{
	TPoint &lt;lhs_count&gt; lhs;
	lhs.copy &lt;rhs_count&gt; (rhs.value);
	return lhs;
}

template &lt;size_t ct&gt;
std::istream&amp; operator &gt;&gt; (std::istream &amp;s, TPoint&lt;ct&gt; &amp;point)
{
	for (size_t i (0); i != ct; ++i)
	{
		s &gt;&gt; point[i];
	}
	return s;
}

template &lt;size_t ct&gt;
std::ostream&amp; operator &lt;&lt; (std::ostream &amp;s, const TPoint&lt;ct&gt; &amp;point)
{
	s &lt;&lt; point[0];
	for (size_t i(1), e(ct); i &lt; e; ++i)
	{
		s &lt;&lt; &quot;;&quot; &lt;&lt; point[i];
	}
	return s;
}

template &lt;size_t dim1, size_t dim2&gt;
TPoint&lt;dim1&gt; operator + (const TPoint&lt;dim1&gt;&amp; lhs, const TPoint&lt;dim2&gt;&amp; rhs)
{
	TPoint&lt;dim1&gt; tmp = Point &lt;dim1, dim2&gt; (lhs);
	return tmp += rhs;
}

template &lt;size_t dim1, size_t dim2&gt;
TPoint&lt;dim1&gt; operator - (const TPoint&lt;dim1&gt;&amp; lhs, const TPoint&lt;dim2&gt;&amp; rhs)
{
	return lhs + -rhs;
}
</code></pre>
<p>Falls jemand eine Meinung dazu hat, würde ich diese natürlich auch gern (hier) lesen ^^<br />
Die op += und -= sind mit Absicht nicht für andere (also verschiedene) Dimensionen implementiert...<br />
Mir schon klar, dass da noch ne ganze Menge fehlt (Abstandsberechnung, Skalarmultiplikation, ...) - aber denke, dass sie so doch schon rel. viel kann, was man von einer Klasse für Punkte erwarten könnte ^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629474</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629474</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Fri, 12 Dec 2008 13:19:56 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Fri, 12 Dec 2008 19:07:29 GMT]]></title><description><![CDATA[<p>Tip: du kannst das was du mit deinem ursprünglichen &quot;swap&quot; Template (später &quot;copy&quot;) wolltest schon machen, nur ist die Syntax anders als du es probiert hast:</p>
<pre><code class="language-cpp">template &lt;size_t Tct_rhs&gt;
    void copy (const float (&amp;first)[Tct_rhs]) // sieht komisch aus, geht aber
    {
        // ...
    }
</code></pre>
<p>Du kannst copy dann auch ohne expliziter Angabe der Grösse aufrufen, und der Compiler checkt ob die Grösse auch stimmt:</p>
<pre><code class="language-cpp">void test()
    {
        float foo[42];
        copy(foo); // ruft copy&lt;42&gt;(foo) auf
        copy&lt;99&gt;(foo); // Fehler
    }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1629657</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629657</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 12 Dec 2008 19:07:29 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Fri, 12 Dec 2008 20:05:49 GMT]]></title><description><![CDATA[<p>Jopp, das ich das kann, weiß ich - nur dummerweise konnte der MSVC 2008 die Templates nicht von allein auflösen - also mustse ich ihm ein wenig Hilfe geben - indem ich ihm die Anzhl gegeben hab - und dann dachte ich mir, dass ich ihm dann auch gleich nur nen Pointer übergeben kann - aber ok, dann kann ichs auch mit der Referenz machen, wie du sie mir geschickt hast - danke : &gt;</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629686</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629686</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Fri, 12 Dec 2008 20:05:49 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Sat, 13 Dec 2008 01:27:35 GMT]]></title><description><![CDATA[<blockquote>
<p>nur dummerweise konnte der MSVC 2008 die Templates nicht von allein auflösen - also mustse ich ihm ein wenig Hilfe geben - indem ich ihm die Anzhl gegeben hab</p>
</blockquote>
<p>MSVC 2008 kann das sehr schön auflösen. Klappt eigentlich seit MSVC 2003. Bei VC 6 bin ich mir nicht sicher, kann leicht sein dass der das noch nicht checkt.</p>
<p>Das Problem ist dass in deinem Beispiel die Referenz fehlt, deswegen geht es nicht. Die Syntax die du verwendet hast ist nur die Umständliche Version ein Parameter des Typs T* zu schreiben. Soll heissen:</p>
<pre><code class="language-cpp">void foo(T param[10]);
// ist genau dasselbe wie
void foo(T param[]);
// und auch genau dasselbe wie
void foo(T* param);
</code></pre>
<p>Lässt sich auch schön zeigen durch ein kleines Testprogramm:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

void foo1(int param[10]) {}
void foo2(int param[20]) {}
void foo3(int param[]) {}
void foo4(int* param) {}

template &lt;class T&gt; struct test
{
    static int bar;
    static char const* get_name()
    {
        return typeid(T).name();
    }
};

template &lt;class T&gt; int test&lt;T&gt;::bar = 0;

template &lt;class T&gt; void test2(T)
{
    std::cout &lt;&lt; &quot;type: &quot; &lt;&lt; test&lt;T&gt;::get_name() &lt;&lt; &quot;\n&quot;;
    test&lt;T&gt;::bar++;
    std::cout &lt;&lt; &quot;bar = &quot; &lt;&lt; test&lt;T&gt;::bar &lt;&lt; &quot;\n&quot;;
}

int main()
{
    test2(&amp;foo1);
    test2(&amp;foo2);
    test2(&amp;foo3);
    test2(&amp;foo4);

    int i = 0;
    foo1(&amp;i);  // foo1 ist &quot;void foo1(int param[10])&quot; - trotzdem gibt das keinen fehler,
               // weil die 10 in &quot;[10]&quot; vollkommen &quot;ignoriert&quot; wird, und &quot;param&quot;
               // hier eben letztlich nur ein einfacher Zeiger ist

    return 0;
}
</code></pre>
<p>Die &quot;[]&quot; Schreibweise für Zeiger-Parameter ist IMO sehr verwirrend - viele Leute erwarten sich etwas ganz anderes wenn sie das schreiben, bzw. auch wenn sie es lesen. Deswegn ist das auch etwas was ich nie nie niemals verwende.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629796</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629796</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 13 Dec 2008 01:27:35 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Sat, 13 Dec 2008 10:54:22 GMT]]></title><description><![CDATA[<p>Hmm.. interessantes Beispiel - gut - beim copy-Aufruf gibt es wirklich keinen Fehler mehr, wenn ich das Template-Argument net explizit mit angebe - wobei ich schwören könnte, dass es noch gestern nicht ging - naja, hab ich mich mal wieder geirrt ^^</p>
<p>Aber bei Point &lt;x, y&gt; brauch ich sie auf jeden Fall - wobei man das ja wiederrum auch durchaus positiv untermauern könnte - aber ein wenig nerven tuts ja schon ^^</p>
<pre><code class="language-cpp">TPoint &lt;3&gt; a;
a[0] = 1;
a[1] = 2;
a[2] = 3;

TPoint &lt;2&gt; b = Point &lt;2, 3&gt; (a);
TPoint &lt;2&gt; B (b);

TPoint &lt;3&gt; A(Point &lt;3, 2&gt; (B));
</code></pre>
<pre><code class="language-cpp">template &lt;size_t lhs_count, size_t rhs_count&gt;
TPoint &lt;lhs_count&gt; Point (const TPoint &lt;rhs_count&gt; &amp;rhs)
{
	TPoint &lt;lhs_count&gt; lhs;
	lhs.copy (rhs.value);
	return lhs;
}
</code></pre>
<p>bzw.</p>
<pre><code class="language-cpp">template &lt;size_t Tct&gt;
class TPoint
{
	template &lt;size_t Tct_rhs&gt;
	void copy ( const float (&amp;first)[Tct_rhs] )
	{
		if(dimension &lt;= Tct_rhs)
		{
			std::copy (first, first+dimension, value);
		}
		else	//Tct_rhs &lt; dim
		{
			std::copy (first, first+Tct_rhs, value);
			std::fill (value+Tct_rhs+1, value+dimension, 0.f);
		}
	}
};
</code></pre>
<p>Danke ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1629855</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1629855</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sat, 13 Dec 2008 10:54:22 GMT</pubDate></item><item><title><![CDATA[Reply to Class1 cannot access private member declared in Class1 on Sun, 14 Dec 2008 08:25:26 GMT]]></title><description><![CDATA[<p>Klar musst du bei &quot;Point&lt;x, y&gt;&quot; noch was angeben, da x ja nur im Return-Typ vorkommt - das kann der Compiler logischerweise nicht anhand der Parameter erraten.<br />
Du musst aber &quot;y&quot; nicht mit angeben, also &quot;Point&lt;2&gt;(a)&quot; sollte in deinem Beispiel reichen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630217</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630217</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 14 Dec 2008 08:25:26 GMT</pubDate></item></channel></rss>