<?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[Was mach ich hier falsch? (source inside)]]></title><description><![CDATA[<p>Hallo,<br />
Ich komme mir richtig dumm vor, aber irgendwie bekomme ich in C++ nicht die einfachsten Sachen hin, und dass obwohl ich in Java fast zu den Gurus gehöre. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Ich habe hier 2 Klassen Object und String, wobei String von Object erbt und Object eine Funktion bereitstellt, welche einen String zurück gibt. Ja eine wunderschöne zirkuläre Abhängigkeit. Nur wie löse ich die richtig auf? Momentan bekomme ich jedenfalls folgende Fehlermeldung von gcc:</p>
<pre><code>g++ -fPIC -g -Wall -c -o src/de/jtdev/base/Object.o src/de/jtdev/base/Object.cpp
src/de/jtdev/base/Object.cpp:11: Fehler: virtuelle Deklaration einer äußeren Klasse
src/de/jtdev/base/Object.cpp:14: Fehler: virtuelle Deklaration einer äußeren Klasse
src/de/jtdev/base/Object.cpp: In member function »virtual de_jtdev_base::String* de_jtdev_base::Object::toString() const«:
src/de/jtdev/base/Object.cpp:15: Fehler: invalid use of undefined type »struct de_jtdev_base::String«
src/de/jtdev/base/Object.hpp:14: Fehler: forward declaration of »struct de_jtdev_base::String«
</code></pre>
<p>Dazu hier die 4 Sourcen der Klassen:</p>
<p>Object.hpp:</p>
<pre><code class="language-cpp">#ifndef DE_JTDEV_BASE_OBJECT_HPP
#define DE_JTDEV_BASE_OBJECT_HPP

#include &lt;stdio.h&gt;

namespace de_jtdev_base {

/*
template&lt;class CheckType, InstanceType&gt;
bool isInstanceOf(InstanceType &amp;instance) {
	return (dynamic_cast&lt;CheckType*&gt;(&amp;instance) != NULL);
}*/

class String;

class Object {

	public:
		Object();
		virtual ~Object();
		virtual String* toString() const;

}; // class Object
} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_OBJECT_HPP
</code></pre>
<p>Object.cpp:</p>
<pre><code class="language-cpp">#ifndef DE_JTDEV_BASE_OBJECT_CPP
#define DE_JTDEV_BASE_OBJECT_CPP

#include &quot;Object.hpp&quot;

namespace de_jtdev_base {

Object::Object() {
}

virtual Object::~Object() {
}

virtual String* Object::toString() const {
	return new String();
}

} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_OBJECT_CPP
</code></pre>
<p>String.hpp:</p>
<pre><code class="language-cpp">#ifndef DE_JTDEV_BASE_STRING_HPP
#define DE_JTDEV_BASE_STRING_HPP

#include &lt;sys/types.h&gt;
#include &quot;Object.hpp&quot;

namespace de_jtdev_base {

typedef unsigned short character;

class String : public Object {

	private:
		character* data;
		size_t length;

	public:
		String();
		String(const String &amp;original);
		String(const character* data, const size_t length);
		String(const character* data, const size_t offset, const size_t count);
		~String();

		size_t getLength() const;

}; // class String
} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_STRING_HPP
</code></pre>
<p>String.cpp:</p>
<pre><code class="language-cpp">#ifndef DE_JTDEV_BASE_STRING_CPP
#define DE_JTDEV_BASE_STRING_CPP

#include &lt;string.h&gt;
#include &quot;String.hpp&quot;

namespace de_jtdev_base {

String::String() {
	length = 0;
	data = new character[0];
}

String::String(const String &amp;original) {
	length = original.length;
	data = new character[length];
	memcpy(data, original.data, sizeof(character)*length);
}

String::String(const character* _data, const size_t _length) {
	length = _length;
	data = new character[length];
	memcpy(data, _data, sizeof(character)*length);
}

String::String(const character* _data, const size_t _offset, const size_t _length) {
	length = _length;
	data = new character[length];
	memcpy(data, (const character*)(_data + _offset), sizeof(character)*length);
}

String::~String() {
	delete data;
}

size_t String::getLength() const {
	return length;
}

} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_STRING_CPP
</code></pre>
<p>Ist ja eigentlich nichts so besonderes, nur was bemängelt die gcc da? Ich werde aus dieser Fehlermeldung irgendwie nicht schlau... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Hoffe ihr habt genug Ausdauer um euch da durchzufressen, ich platze hier jedenfalls gleich.</p>
<p>mfG<br />
Tobain</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/148050/was-mach-ich-hier-falsch-source-inside</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Sep 2026 11:23:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/148050.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 22 May 2006 20:09:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Was mach ich hier falsch? (source inside) on Mon, 22 May 2006 20:09:19 GMT]]></title><description><![CDATA[<p>Hallo,<br />
Ich komme mir richtig dumm vor, aber irgendwie bekomme ich in C++ nicht die einfachsten Sachen hin, und dass obwohl ich in Java fast zu den Gurus gehöre. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Ich habe hier 2 Klassen Object und String, wobei String von Object erbt und Object eine Funktion bereitstellt, welche einen String zurück gibt. Ja eine wunderschöne zirkuläre Abhängigkeit. Nur wie löse ich die richtig auf? Momentan bekomme ich jedenfalls folgende Fehlermeldung von gcc:</p>
<pre><code>g++ -fPIC -g -Wall -c -o src/de/jtdev/base/Object.o src/de/jtdev/base/Object.cpp
src/de/jtdev/base/Object.cpp:11: Fehler: virtuelle Deklaration einer äußeren Klasse
src/de/jtdev/base/Object.cpp:14: Fehler: virtuelle Deklaration einer äußeren Klasse
src/de/jtdev/base/Object.cpp: In member function »virtual de_jtdev_base::String* de_jtdev_base::Object::toString() const«:
src/de/jtdev/base/Object.cpp:15: Fehler: invalid use of undefined type »struct de_jtdev_base::String«
src/de/jtdev/base/Object.hpp:14: Fehler: forward declaration of »struct de_jtdev_base::String«
</code></pre>
<p>Dazu hier die 4 Sourcen der Klassen:</p>
<p>Object.hpp:</p>
<pre><code class="language-cpp">#ifndef DE_JTDEV_BASE_OBJECT_HPP
#define DE_JTDEV_BASE_OBJECT_HPP

#include &lt;stdio.h&gt;

namespace de_jtdev_base {

/*
template&lt;class CheckType, InstanceType&gt;
bool isInstanceOf(InstanceType &amp;instance) {
	return (dynamic_cast&lt;CheckType*&gt;(&amp;instance) != NULL);
}*/

class String;

class Object {

	public:
		Object();
		virtual ~Object();
		virtual String* toString() const;

}; // class Object
} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_OBJECT_HPP
</code></pre>
<p>Object.cpp:</p>
<pre><code class="language-cpp">#ifndef DE_JTDEV_BASE_OBJECT_CPP
#define DE_JTDEV_BASE_OBJECT_CPP

#include &quot;Object.hpp&quot;

namespace de_jtdev_base {

Object::Object() {
}

virtual Object::~Object() {
}

virtual String* Object::toString() const {
	return new String();
}

} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_OBJECT_CPP
</code></pre>
<p>String.hpp:</p>
<pre><code class="language-cpp">#ifndef DE_JTDEV_BASE_STRING_HPP
#define DE_JTDEV_BASE_STRING_HPP

#include &lt;sys/types.h&gt;
#include &quot;Object.hpp&quot;

namespace de_jtdev_base {

typedef unsigned short character;

class String : public Object {

	private:
		character* data;
		size_t length;

	public:
		String();
		String(const String &amp;original);
		String(const character* data, const size_t length);
		String(const character* data, const size_t offset, const size_t count);
		~String();

		size_t getLength() const;

}; // class String
} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_STRING_HPP
</code></pre>
<p>String.cpp:</p>
<pre><code class="language-cpp">#ifndef DE_JTDEV_BASE_STRING_CPP
#define DE_JTDEV_BASE_STRING_CPP

#include &lt;string.h&gt;
#include &quot;String.hpp&quot;

namespace de_jtdev_base {

String::String() {
	length = 0;
	data = new character[0];
}

String::String(const String &amp;original) {
	length = original.length;
	data = new character[length];
	memcpy(data, original.data, sizeof(character)*length);
}

String::String(const character* _data, const size_t _length) {
	length = _length;
	data = new character[length];
	memcpy(data, _data, sizeof(character)*length);
}

String::String(const character* _data, const size_t _offset, const size_t _length) {
	length = _length;
	data = new character[length];
	memcpy(data, (const character*)(_data + _offset), sizeof(character)*length);
}

String::~String() {
	delete data;
}

size_t String::getLength() const {
	return length;
}

} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_STRING_CPP
</code></pre>
<p>Ist ja eigentlich nichts so besonderes, nur was bemängelt die gcc da? Ich werde aus dieser Fehlermeldung irgendwie nicht schlau... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Hoffe ihr habt genug Ausdauer um euch da durchzufressen, ich platze hier jedenfalls gleich.</p>
<p>mfG<br />
Tobain</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1063096</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1063096</guid><dc:creator><![CDATA[Tobain]]></dc:creator><pubDate>Mon, 22 May 2006 20:09:19 GMT</pubDate></item><item><title><![CDATA[Reply to Was mach ich hier falsch? (source inside) on Mon, 22 May 2006 20:22:15 GMT]]></title><description><![CDATA[<p>In Object.cpp musst du String.h inkludieren, weil du eine Instanz von String erstellst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1063103</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1063103</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Mon, 22 May 2006 20:22:15 GMT</pubDate></item><item><title><![CDATA[Reply to Was mach ich hier falsch? (source inside) on Mon, 22 May 2006 20:31:39 GMT]]></title><description><![CDATA[<p>So habe ich jetzt einfach mal so getan und #include &quot;String.hpp&quot; zu Object.cpp hinzugefügt, nur bekomme ich jetzt folgende Fehlermeldung:</p>
<pre><code>src/de/jtdev/base/Object.cpp:12: Fehler: virtuelle Deklaration einer äußeren Klasse
src/de/jtdev/base/Object.cpp:15: Fehler: virtuelle Deklaration einer äußeren Klasse
</code></pre>
<p>Was soll hier eine &quot;virtuelle Deklaration einer äußeren Klasse&quot; sein?<br />
Warum sollte ich die Includeanweisung nicht zu Headerdatei hinzufügen?</p>
<p>Trotzdem schon mal vielen dank, denn es sind jetzt schon wesentlich weniger Fehlermeldungen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1063109</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1063109</guid><dc:creator><![CDATA[Tobain]]></dc:creator><pubDate>Mon, 22 May 2006 20:31:39 GMT</pubDate></item><item><title><![CDATA[Reply to Was mach ich hier falsch? (source inside) on Mon, 22 May 2006 20:39:06 GMT]]></title><description><![CDATA[<p>Tobain schrieb:</p>
<blockquote>
<p>Ich komme mir richtig dumm vor, aber irgendwie bekomme ich in C++ nicht die einfachsten Sachen hin, und dass obwohl ich in Java fast zu den Gurus gehöre.</p>
</blockquote>
<p>s/obwohl/weil/ dann passt's :p</p>
<p>1. &quot;virtual&quot; bei der Deklaration und bei der Implementation zu benutzen lassen wir mal sein <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="😉"
    /><br />
2. Du möchtest in Object.cüü ein '#include &quot;String.hpp&quot;' einfügen. Durch Object.hpp hast du zwar die forward declaration drin, aber nicht die vollständige Deklaration. Bei einer forward declaration ohne vollständiger Deklaration kann etwas nicht stimmen, und da springt dir der G++ ins Gesicht.</p>
<p>Mit folgendem Code gates dann bei mir (G++ 4.0.3):</p>
<pre><code class="language-cpp">// Object.hpp
#ifndef DE_JTDEV_BASE_OBJECT_HPP
#define DE_JTDEV_BASE_OBJECT_HPP

#include &lt;stdio.h&gt;

namespace de_jtdev_base {

/*
template&lt;class CheckType, InstanceType&gt;
bool isInstanceOf(InstanceType &amp;instance) {
    return (dynamic_cast&lt;CheckType*&gt;(&amp;instance) != NULL);
}*/

class String;

class Object {

    public:
        Object();
        virtual ~Object();
        virtual String* toString() const;

}; // class Object
} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_OBJECT_HPP
</code></pre>
<pre><code class="language-cpp">// Object.cpp
#ifndef DE_JTDEV_BASE_OBJECT_CPP
#define DE_JTDEV_BASE_OBJECT_CPP

#include &quot;Object.hpp&quot;
#include &quot;String.hpp&quot;

namespace de_jtdev_base {

Object::Object() {
}

Object::~Object() {
}

String* Object::toString() const {
    return new String();
}

} // namespace de_jtdev_base

#endif // DE_JTDEV_BASE_OBJECT_CPP
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1063110</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1063110</guid><dc:creator><![CDATA[tommie-lie]]></dc:creator><pubDate>Mon, 22 May 2006 20:39:06 GMT</pubDate></item><item><title><![CDATA[Reply to Was mach ich hier falsch? (source inside) on Mon, 22 May 2006 20:37:45 GMT]]></title><description><![CDATA[<p>Da wird man mal kurz abgelenkt und schon war jemand anders schneller...</p>
<p>Tobain schrieb:</p>
<blockquote>
<p>Warum sollte ich die Includeanweisung nicht zu Headerdatei hinzufügen?</p>
</blockquote>
<p>Dann müsstest du sie *nach* der Deklaration von Object einfügen, weil String.hpp von Object.hpp abhängt.<br />
Man hätte also folgende Möglichkeiten:</p>
<pre><code class="language-cpp">#include &quot;sonstigeincludes&quot;

class String;

class Object
{
// ...
}

#include &quot;String.hpp&quot;

// oder

#include &quot;sonstigeincludes&quot;

class Object;

#include &quot;String.hpp&quot;

class Object
{
// ...
}
</code></pre>
<p>Oder eben das, was ich in meinem vorherigen Beitrag gezeigt habt. Und bevor ich mir meine Include-Anweisungen sonstwo im Quellcode verteile, stecke ich sie lieber zusammen in eine Implementationsdatei.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1063112</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1063112</guid><dc:creator><![CDATA[tommie-lie]]></dc:creator><pubDate>Mon, 22 May 2006 20:37:45 GMT</pubDate></item></channel></rss>