<?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[Stack Class Problem [error C2248: &amp;quot;stack::stack&amp;quot; : Kein Zugriff auf private Element, dessen Deklaration in der]]></title><description><![CDATA[<p>Hallo alle,<br />
ich Programmiere schon eine ganze Zeit lang in C und habe mich eben entschieden C++ zu lernen (ich muss mich fragen warum ich diesen Himmel nicht schon ehr gesehen habe). Also habe ich mir erst einmal ein Tutorial aus eurer schönen Tutorialkiste genommen und mir die ersten 4 Kapitel durchgelesen und dachte mir: &quot;So mal gucken jetzt weiß ich was Klassen sind Konstruktoren und die ganzen anfänglichen Anfänger Sachen, wollen wir mal unser Wissen ausprobieren.&quot;. Also fange ich an eine Stack-Klasse zu schreiben funktioniert auch super, nur der compiler findet meinen Source nicht schön, woran könnte es liegen? (VC++)</p>
<p>Fehlermeldung:<br />
main.cpp(7) : error C2248: &quot;stack::stack&quot; : Kein Zugriff auf private Element, dessen Deklaration in der Klasse &quot;stack&quot; erfolgte<br />
stack.h(5) : Siehe Deklaration von 'stack::stack'</p>
<pre><code class="language-cpp">//Meine stack.h
#ifndef __STACK_H
#define __STACK_H

class stack {
	stack() {
		_stack = new int[50];
		_max = 50;
	}

	public:
		void push(int);
		int pop(void);
		int count(void);
	private:
		void stack_size_check(void);
		int *_stack;
		int _count;
		int _max;
};

#endif
</code></pre>
<pre><code class="language-cpp">//Meine stack.cpp
#include &lt;iostream&gt;
#include &quot;stack.h&quot;

using namespace std;

int stack::count(void) {
	cout &lt;&lt; _count &lt;&lt; endl;
	return 0;
}

void stack::push(int new_var) {
	stack_size_check();
	_count++;
	_stack[_count] = new_var;
}

int stack::pop() {
	_count--;
	return _stack[_count+1];
}

void stack::stack_size_check(){

	// gibt es in C++ eine Art relloc?
	if(_max&lt;(_count+1)) {
		int *temp = new int[_max];

		for(int i=0; i&lt;=_max; i++) {
			temp[i] = _stack[i];
		}

		_max += 50;
		_stack = new int[_max];

		for(i=0; i&lt;=_max; i++) {
			_stack[i] = temp[i];
		}
	}

}
</code></pre>
<pre><code class="language-cpp">//meine main.cpp
#include &lt;iostream&gt;
#include &quot;stack.h&quot;

using namespace std;

int main(void) {
	stack stack1;
	stack1.push(5);

	return 0;
}
</code></pre>
<p>danke im voraus</p>
<p>p.s. ist der Source so für C++ in Ordnung? (abgesehen von meinem Fehler)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/217336/stack-class-problem-error-c2248-quot-stack-stack-quot-kein-zugriff-auf-private-element-dessen-deklaration-in-der</link><generator>RSS for Node</generator><lastBuildDate>Sat, 10 Oct 2026 17:40:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/217336.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 Jul 2008 15:37:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Stack Class Problem [error C2248: &amp;quot;stack::stack&amp;quot; : Kein Zugriff auf private Element, dessen Deklaration in der on Sun, 06 Jul 2008 15:37:07 GMT]]></title><description><![CDATA[<p>Hallo alle,<br />
ich Programmiere schon eine ganze Zeit lang in C und habe mich eben entschieden C++ zu lernen (ich muss mich fragen warum ich diesen Himmel nicht schon ehr gesehen habe). Also habe ich mir erst einmal ein Tutorial aus eurer schönen Tutorialkiste genommen und mir die ersten 4 Kapitel durchgelesen und dachte mir: &quot;So mal gucken jetzt weiß ich was Klassen sind Konstruktoren und die ganzen anfänglichen Anfänger Sachen, wollen wir mal unser Wissen ausprobieren.&quot;. Also fange ich an eine Stack-Klasse zu schreiben funktioniert auch super, nur der compiler findet meinen Source nicht schön, woran könnte es liegen? (VC++)</p>
<p>Fehlermeldung:<br />
main.cpp(7) : error C2248: &quot;stack::stack&quot; : Kein Zugriff auf private Element, dessen Deklaration in der Klasse &quot;stack&quot; erfolgte<br />
stack.h(5) : Siehe Deklaration von 'stack::stack'</p>
<pre><code class="language-cpp">//Meine stack.h
#ifndef __STACK_H
#define __STACK_H

class stack {
	stack() {
		_stack = new int[50];
		_max = 50;
	}

	public:
		void push(int);
		int pop(void);
		int count(void);
	private:
		void stack_size_check(void);
		int *_stack;
		int _count;
		int _max;
};

#endif
</code></pre>
<pre><code class="language-cpp">//Meine stack.cpp
#include &lt;iostream&gt;
#include &quot;stack.h&quot;

using namespace std;

int stack::count(void) {
	cout &lt;&lt; _count &lt;&lt; endl;
	return 0;
}

void stack::push(int new_var) {
	stack_size_check();
	_count++;
	_stack[_count] = new_var;
}

int stack::pop() {
	_count--;
	return _stack[_count+1];
}

void stack::stack_size_check(){

	// gibt es in C++ eine Art relloc?
	if(_max&lt;(_count+1)) {
		int *temp = new int[_max];

		for(int i=0; i&lt;=_max; i++) {
			temp[i] = _stack[i];
		}

		_max += 50;
		_stack = new int[_max];

		for(i=0; i&lt;=_max; i++) {
			_stack[i] = temp[i];
		}
	}

}
</code></pre>
<pre><code class="language-cpp">//meine main.cpp
#include &lt;iostream&gt;
#include &quot;stack.h&quot;

using namespace std;

int main(void) {
	stack stack1;
	stack1.push(5);

	return 0;
}
</code></pre>
<p>danke im voraus</p>
<p>p.s. ist der Source so für C++ in Ordnung? (abgesehen von meinem Fehler)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1541939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1541939</guid><dc:creator><![CDATA[Neuling_]]></dc:creator><pubDate>Sun, 06 Jul 2008 15:37:07 GMT</pubDate></item><item><title><![CDATA[Reply to Stack Class Problem [error C2248: &amp;quot;stack::stack&amp;quot; : Kein Zugriff auf private Element, dessen Deklaration in der on Sun, 06 Jul 2008 15:56:24 GMT]]></title><description><![CDATA[<p>Der Konstruktor muss auch öffentlich ( <code>public</code> ) sein, wenn du Instanzen erstellen möchtest. Im Moment ist er bei dir <code>private</code> .</p>
<p>Wenn du im Konstruktor mit <code>new</code> dynamischen Speicher anforderst, musst du diesen im Destruktor mit <code>delete</code> wieder freigeben. Ausserdem solltest du Kopierkonstruktor und Zuweisungsoperator überladen. Deshalb rate ich dir, gerade am Anfang nur Stack-Variablen mit statischer Speicherverwaltung zu haben.</p>
<p>Neuling_ schrieb:</p>
<blockquote>
<pre><code class="language-cpp">#ifndef __STACK_H
#define __STACK_H
</code></pre>
</blockquote>
<p>Du solltest Bezeichner nicht mit &quot;__&quot; oder &quot;_&quot; und Grossbuchstaben beginnen. Siehe dazu <a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39461.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-39461.html</a></p>
<p>Neuling schrieb:</p>
<blockquote>
<pre><code class="language-cpp">void push(int);
int pop(void);
int count(void);
</code></pre>
</blockquote>
<p><code>void</code> brauchst du in C++ nicht als Argument zu schreiben. Oben würde ich dafür zu raten, auch den Bezeichner hinzustellen, also:</p>
<pre><code class="language-cpp">void push(int new_int);
int pop();
int count();
</code></pre>
<p>Das Gleiche gilt auch für <code>main()</code> :</p>
<pre><code class="language-cpp">int main() {
	stack stack1;
	stack1.push(5);
}
</code></pre>
<p><code>return 0;</code> ist überflüssig, am Ende von <code>main()</code> wird automatisch 0 zurückgegeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1541946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1541946</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 06 Jul 2008 15:56:24 GMT</pubDate></item></channel></rss>