<?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[abgeleitete Klasse variable aus der Basisklasse nicht deklariert]]></title><description><![CDATA[<p>Hey Leute,</p>
<p>Übe gerade ein bisschen Cpp und kann mir den Grund für die Fehlermeldung des Compilers nicht erklären:<br />
<strong>|71|error: ‘first’ was not declared in this scope|</strong><br />
Wenn ich aber stattdessen einfach this-&gt;first schreibe, läuft alles.<br />
Warum ist das so? Schließlich ist Set doch von der Klasse Liste abgeleitet und sollte dadruch Zugriff auf die public und protected Bestandteile haben. Und mit this-&gt; ändere ich ja auch nichts an den Zugriffsrechten oder ähnliches, oder?</p>
<p>Danke für eure Hilfe und Tipps <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="🙂"
    /></p>
<pre><code>#include &lt;iostream&gt;

template &lt;typename T&gt;
struct Knoten{
	T value;
	Knoten&lt;T&gt; *next;

	Knoten(T v = 0){
		next = NULL;
		value = v;
	}
};

template &lt;typename T&gt;
class Liste{
protected:
	Knoten&lt;T&gt; *first;
	int size;
public:
	Liste(){
		size = 0;
		first = new Knoten&lt;T&gt;();
	}
	~Liste(){
		while(first != NULL){
			Knoten&lt;T&gt; *k = first-&gt;next;
			delete first;
			first = k;
		}
	}
	void insert(T v){
        Knoten&lt;T&gt; *temp, *temp2;
        temp = first;
        while(temp != NULL &amp;&amp; v &gt; temp-&gt;value){
            temp2 = temp;
            temp = temp-&gt;next;
        }
        if(temp == NULL){
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
        } else{
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
            temp2-&gt;next-&gt;next = temp;
        }
	}
	void printListe(){
        Knoten&lt;T&gt; *temp;
        temp = first;
        while(temp != NULL){
            std::cout &lt;&lt; temp-&gt;value &lt;&lt; std::endl;
            temp = temp-&gt;next;
        }
	}
	T operator[](const int &amp;pos){
        Knoten&lt;T&gt; *temp = first;
        for(int i = 0; i &lt; pos &amp;&amp; temp != NULL; i++){
            temp = temp-&gt;next;
        }
        if(temp == NULL){
            throw &quot;Out Of Range&quot;;
        } else{
            return temp-&gt;value;
        }
	}
};

template &lt;typename T&gt;
class Set : public Liste&lt;T&gt;{
public:
    void insert(T v){
        Knoten&lt;T&gt; *temp, *temp2;
        temp = first;
        while(temp != NULL &amp;&amp; v &gt; temp-&gt;value){
            temp2 = temp;
            temp = temp-&gt;next;
        }
        if(temp == NULL){
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
        } else if(temp-&gt;value != v){
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
            temp2-&gt;next-&gt;next = temp;
        }
    }
};

int main(){
    Set&lt;double&gt; test;
    test.insert(2.2);
    test.insert(5.5);
    test.insert(7.7);
    test.insert(4.4);
    test.insert(10.1);
    test.insert(7.7);
    test.printListe();
    std::cout &lt;&lt; &quot;Position 3: &quot; &lt;&lt; test[3] &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;Position 0: &quot; &lt;&lt; test[0] &lt;&lt; std::endl;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/334324/abgeleitete-klasse-variable-aus-der-basisklasse-nicht-deklariert</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 19:17:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/334324.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 Sep 2015 14:09:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to abgeleitete Klasse variable aus der Basisklasse nicht deklariert on Sun, 06 Sep 2015 14:09:04 GMT]]></title><description><![CDATA[<p>Hey Leute,</p>
<p>Übe gerade ein bisschen Cpp und kann mir den Grund für die Fehlermeldung des Compilers nicht erklären:<br />
<strong>|71|error: ‘first’ was not declared in this scope|</strong><br />
Wenn ich aber stattdessen einfach this-&gt;first schreibe, läuft alles.<br />
Warum ist das so? Schließlich ist Set doch von der Klasse Liste abgeleitet und sollte dadruch Zugriff auf die public und protected Bestandteile haben. Und mit this-&gt; ändere ich ja auch nichts an den Zugriffsrechten oder ähnliches, oder?</p>
<p>Danke für eure Hilfe und Tipps <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="🙂"
    /></p>
<pre><code>#include &lt;iostream&gt;

template &lt;typename T&gt;
struct Knoten{
	T value;
	Knoten&lt;T&gt; *next;

	Knoten(T v = 0){
		next = NULL;
		value = v;
	}
};

template &lt;typename T&gt;
class Liste{
protected:
	Knoten&lt;T&gt; *first;
	int size;
public:
	Liste(){
		size = 0;
		first = new Knoten&lt;T&gt;();
	}
	~Liste(){
		while(first != NULL){
			Knoten&lt;T&gt; *k = first-&gt;next;
			delete first;
			first = k;
		}
	}
	void insert(T v){
        Knoten&lt;T&gt; *temp, *temp2;
        temp = first;
        while(temp != NULL &amp;&amp; v &gt; temp-&gt;value){
            temp2 = temp;
            temp = temp-&gt;next;
        }
        if(temp == NULL){
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
        } else{
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
            temp2-&gt;next-&gt;next = temp;
        }
	}
	void printListe(){
        Knoten&lt;T&gt; *temp;
        temp = first;
        while(temp != NULL){
            std::cout &lt;&lt; temp-&gt;value &lt;&lt; std::endl;
            temp = temp-&gt;next;
        }
	}
	T operator[](const int &amp;pos){
        Knoten&lt;T&gt; *temp = first;
        for(int i = 0; i &lt; pos &amp;&amp; temp != NULL; i++){
            temp = temp-&gt;next;
        }
        if(temp == NULL){
            throw &quot;Out Of Range&quot;;
        } else{
            return temp-&gt;value;
        }
	}
};

template &lt;typename T&gt;
class Set : public Liste&lt;T&gt;{
public:
    void insert(T v){
        Knoten&lt;T&gt; *temp, *temp2;
        temp = first;
        while(temp != NULL &amp;&amp; v &gt; temp-&gt;value){
            temp2 = temp;
            temp = temp-&gt;next;
        }
        if(temp == NULL){
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
        } else if(temp-&gt;value != v){
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
            temp2-&gt;next-&gt;next = temp;
        }
    }
};

int main(){
    Set&lt;double&gt; test;
    test.insert(2.2);
    test.insert(5.5);
    test.insert(7.7);
    test.insert(4.4);
    test.insert(10.1);
    test.insert(7.7);
    test.printListe();
    std::cout &lt;&lt; &quot;Position 3: &quot; &lt;&lt; test[3] &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;Position 0: &quot; &lt;&lt; test[0] &lt;&lt; std::endl;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2466980</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466980</guid><dc:creator><![CDATA[Cartand]]></dc:creator><pubDate>Sun, 06 Sep 2015 14:09:04 GMT</pubDate></item><item><title><![CDATA[Reply to abgeleitete Klasse variable aus der Basisklasse nicht deklariert on Sun, 06 Sep 2015 14:34:29 GMT]]></title><description><![CDATA[<blockquote>
<p>Und mit this-&gt; ändere ich ja auch nichts an den Zugriffsrechten oder ähnliches, oder?</p>
</blockquote>
<p>Nein, aber du gibst einen expliziten Verweis auf die Klassenvariable. Schau dir mal deinen Funktionscode an:</p>
<pre><code>void insert(T v){
        Knoten&lt;T&gt; *temp, *temp2;
        temp = first;
        while(temp != NULL &amp;&amp; v &gt; temp-&gt;value){
            temp2 = temp;
            temp = temp-&gt;next;
        }
        if(temp == NULL){
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
        } else if(temp-&gt;value != v){
            temp2-&gt;next = new Knoten&lt;T&gt;(v);
            temp2-&gt;next-&gt;next = temp;
        }
</code></pre>
<p>Wenn der <strong>Zugriff ohne this-&gt;</strong> möglich wäre, würde eine Einschränkung resultieren: Du könntest keine lokale Variable mit dem Namen <strong>first</strong> anlegen, z.B</p>
<pre><code>void insert(T v){
        Knoten&lt;T&gt; *temp, *temp2;
        int first = 10;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2466984</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466984</guid><dc:creator><![CDATA[GrüneBanane]]></dc:creator><pubDate>Sun, 06 Sep 2015 14:34:29 GMT</pubDate></item><item><title><![CDATA[Reply to abgeleitete Klasse variable aus der Basisklasse nicht deklariert on Sun, 06 Sep 2015 16:31:05 GMT]]></title><description><![CDATA[<p>Aber das würde ja bedeuten, dass bei diesem Dekorierer Entwurfsmuster ein this-&gt; vor dem dekoriert stehen müsste, schließlich könnte ich in den Klassen Milchschaum und Schoko sonst keine Variable mit der Bezeichnung dekoriert erstellen, oder?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

class Kaffee {
public:
	virtual ~Kaffee() {}
	virtual int preis() = 0;
	virtual std::string rechnung() = 0;
};

class Espresso : public Kaffee {
public:
	int preis(){
		return 240;
	}
	std::string rechnung(){
		return &quot;Espresso: 240\n&quot;;
	}
};

class Hausmischung : public Kaffee {
public:
	int preis(){
		return 180;
	}
	std::string rechnung(){
		return &quot;Hausmischung: 180\n&quot;;
	}
};

class Zutat : public Kaffee {
protected:
	Kaffee *dekoriert;

public:
	Zutat(Kaffee *k){
		dekoriert = k;
	}
};

class Milchschaum : public Zutat {
public:
	Milchschaum(Kaffee *k):Zutat(k){

	}
	~Milchschaum(){
		std::cout &lt;&lt; &quot;~Milchschaum\n&quot;;
		delete dekoriert;
	}
	int preis(){
		return 20 + dekoriert-&gt;preis();
	}
	std::string rechnung(){
		return dekoriert-&gt;rechnung() + &quot;Milchschaum: 20\n&quot;;
	}
};

class Schoko : public Zutat {
public:
	Schoko(Kaffee *k):Zutat(k){

	}
	~Schoko(){
		std::cout &lt;&lt; &quot;~Schoko\n&quot;;
		delete dekoriert;
	}
	int preis(){
		return 10 + dekoriert-&gt;preis();
	}
	std::string rechnung(){
		return dekoriert-&gt;rechnung() + &quot;Schoki: 10\n&quot;;
	}
};

class Keks : public Zutat {
public:
	Keks(Kaffee *k):Zutat(k){

	}
	virtual ~Keks(){
		std::cout &lt;&lt; &quot;~Keks\n&quot;;
		delete dekoriert;
	}
	int preis(){
		return 15 + dekoriert-&gt;preis();
	}
	std::string rechnung(){
		return dekoriert-&gt;rechnung() + &quot;Keks: 15\n&quot;;
	}
};

int main(void) {

	Kaffee *k = new Schoko(
				new Keks(
					new Milchschaum(
						new Espresso())));

	cout &lt;&lt; k-&gt;rechnung();
	cout &lt;&lt; k-&gt;preis() &lt;&lt; endl &lt;&lt; endl;
	delete k;
	cout &lt;&lt; endl &lt;&lt; endl;

	k = new Schoko(
			new Schoko(
				new Milchschaum(
					new Keks(
						new Espresso()))));

	cout &lt;&lt; k-&gt;rechnung();
	cout &lt;&lt; k-&gt;preis() &lt;&lt; endl &lt;&lt; endl;
	delete k;
	cout &lt;&lt; endl &lt;&lt; endl;

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2466988</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466988</guid><dc:creator><![CDATA[Cartand]]></dc:creator><pubDate>Sun, 06 Sep 2015 16:31:05 GMT</pubDate></item><item><title><![CDATA[Reply to abgeleitete Klasse variable aus der Basisklasse nicht deklariert on Sun, 06 Sep 2015 16:36:35 GMT]]></title><description><![CDATA[<p>So, ich habe jetzt einfach mal in das DekoriererMuster templates reingebracht.<br />
Das hatte zur folge, dass ich zum Zugriff von einer abgeleiteten Klasse auf eine Variable in der Basisklasse jetzt this benutzen muss.</p>
<pre><code>template &lt;typename T&gt;
class Schoko : public Zutat&lt;T&gt; {
public:
	Schoko(Kaffee *k):Zutat&lt;T&gt;(k){

	}
	~Schoko(){
		std::cout &lt;&lt; &quot;~Schoko\n&quot;;
		delete this-&gt;dekoriert;
	}
	int preis(){
		return 10 + this-&gt;dekoriert-&gt;preis();
	}
	std::string rechnung(){
		return this-&gt;dekoriert-&gt;rechnung() + &quot;Schoki: 10\n&quot;;
	}
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2466997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466997</guid><dc:creator><![CDATA[Cartand]]></dc:creator><pubDate>Sun, 06 Sep 2015 16:36:35 GMT</pubDate></item><item><title><![CDATA[Reply to abgeleitete Klasse variable aus der Basisklasse nicht deklariert on Sun, 06 Sep 2015 19:03:36 GMT]]></title><description><![CDATA[<p>GrüneBanane schrieb:</p>
<blockquote>
<p>Wenn der <strong>Zugriff ohne this-&gt;</strong> möglich wäre, würde eine Einschränkung resultieren: Du könntest keine lokale Variable mit dem Namen <strong>first</strong> anlegen, z.B</p>
<pre><code>void insert(T v){
        Knoten&lt;T&gt; *temp, *temp2;
        int first = 10;
</code></pre>
</blockquote>
<p>Sorry, Deine Erklärung ist leider falsch. Beim Zugriff auf eine Membervariablen aus einer Memberfunktion ist die explizite Dereferenzierung des Zeigers <strong>this</strong> nicht notwendig. Auch dann nicht, wenn die Membervariable Teil einer Basisklasse ist.</p>
<p>Korrekt aber ist, daß die Derefenzierung erforderlich ist, wenn es auch eine lokale Variable, mit dem selben Namen gibt.</p>
<p>Hier haben wir es mit aber mit Klassentemplates zu tun. Aus einem Grund, den ich jetzt allerdings nicht kenne, verlangt z.B. Gnu-C beim Zugriff auf Memberfunktionen oder -variablen von Basisklassen die explizite Dereferenzierung des Zeigers <strong>this</strong> auch, wenn es keine lokale Variable mit dem selben Namen gibt. Gleiches gilt auch für Aufrufe von Memberfunktionen einer Basisklasse.</p>
<p>Warum das so ist, weiß ich nicht. Meine alten Borland-compiler können sowas problemlos übersetzen.</p>
<p>mfg Martin</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2467013</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2467013</guid><dc:creator><![CDATA[mgaeckler]]></dc:creator><pubDate>Sun, 06 Sep 2015 19:03:36 GMT</pubDate></item><item><title><![CDATA[Reply to abgeleitete Klasse variable aus der Basisklasse nicht deklariert on Sun, 06 Sep 2015 19:41:00 GMT]]></title><description><![CDATA[<p>mgaeckler schrieb:</p>
<blockquote>
<p>Hier haben wir es mit aber mit Klassentemplates zu tun. Aus einem Grund, den ich jetzt allerdings nicht kenne, verlangt z.B. Gnu-C beim Zugriff auf Memberfunktionen oder -variablen von Basisklassen die explizite Dereferenzierung des Zeigers <strong>this</strong> auch, wenn es keine lokale Variable mit dem selben Namen gibt. Gleiches gilt auch für Aufrufe von Memberfunktionen einer Basisklasse.</p>
<p>Warum das so ist, weiß ich nicht. Meine alten Borland-compiler können sowas problemlos übersetzen.</p>
</blockquote>
<p>Das ist verlangt, weil es der Standard so sagt. Borland - wie teilweise auch MSVC - sind da nicht konform.<br />
Der exakte Grund ist hier ausführlich beschrieben: <a href="http://stackoverflow.com/a/4643295" rel="nofollow">http://stackoverflow.com/a/4643295</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2467014</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2467014</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 06 Sep 2015 19:41:00 GMT</pubDate></item><item><title><![CDATA[Reply to abgeleitete Klasse variable aus der Basisklasse nicht deklariert on Mon, 07 Sep 2015 10:56:44 GMT]]></title><description><![CDATA[<p>Ok, danke. Damit hat sich meine Frage aufgeklärt <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2467046</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2467046</guid><dc:creator><![CDATA[Cartand]]></dc:creator><pubDate>Mon, 07 Sep 2015 10:56:44 GMT</pubDate></item></channel></rss>