<?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[Alles korrekt. Trotzdem seltsame Fehler.]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe hier das folgende kleine simple Beispielprogramm:</p>
<pre><code class="language-cpp">// A.cpp
#include &quot;A.hpp&quot;

void A::foo()
{
  b = new B();
  b-&gt;bar();
}

void A::bar()
{
}

// A.hpp
#ifndef __A_HPP__
#define __A_HPP__

#include &quot;B.cpp&quot;

class A
{
public:
  B *b
  void foo();
  void bar();
};

#endif

// B.cpp
#include &quot;B.hpp&quot;

void B::foo()
{
  a = new A();
  a-&gt;bar();
}

void B::bar()
{
}

// B.hpp
#ifndef __B_HPP__
#define __B_HPP__

#include &quot;A.hpp&quot;

class B
{
public:
  A *a;
  void foo();
  void bar();
};

#endif

// main.cpp
#include &quot;A.hpp&quot;
#include &quot;B.hpp&quot;

main()
{
  A a;
  a.foo();
  B b;
  b.foo();
}
</code></pre>
<p>Wenn ich das mit</p>
<pre><code>g++ main.cpp A.cpp B.cpp
</code></pre>
<p>kompileire bekomme ich die folgenden seltsamen Fehlermeldungen:</p>
<pre><code>In file included from B.cpp:2,
                 from A.hpp:5,
                 from main.cpp:2:
B.hpp:10: error: ISO C++ forbids declaration of `A' with no type
B.hpp:10: error: expected `;' before '*' token
In file included from A.hpp:5,
                 from main.cpp:2:
B.cpp: In member function `void B::foo()':
B.cpp:6: error: `a' was not declared in this scope
B.cpp:6: error: `A' has not been declared
In file included from main.cpp:2:
A.hpp: At global scope:
A.hpp:11: error: expected `;' before &quot;void&quot;
main.cpp: In function `int main()':
main.cpp:8: error: 'class A' has no member named 'foo'
In file included from B.cpp:2,
                 from A.hpp:5,
                 from A.cpp:2:
B.hpp:10: error: ISO C++ forbids declaration of `A' with no type
B.hpp:10: error: expected `;' before '*' token
In file included from A.hpp:5,
                 from A.cpp:2:
B.cpp: In member function `void B::foo()':
B.cpp:6: error: `a' was not declared in this scope
B.cpp:6: error: `A' has not been declared
In file included from A.cpp:2:
A.hpp: At global scope:
A.hpp:11: error: expected `;' before &quot;void&quot;
A.cpp:5: error: no `void A::foo()' member function declared in class `A'
A.cpp: In member function `void A::foo()':
A.cpp:6: error: `b' was not declared in this scope
A.cpp: At global scope:
A.cpp:11: error: no `void A::bar()' member function declared in class `A'
A.cpp:11: error: `void A::bar()' and `void A::bar()' cannot be overloaded
In file included from A.hpp:5,
                 from B.hpp:5,
                 from B.cpp:2:
B.cpp:4: error: `B' has not been declared
B.cpp: In function `void foo()':
B.cpp:6: error: `a' was not declared in this scope
B.cpp:6: error: `A' has not been declared
B.cpp: At global scope:
B.cpp:10: error: `B' has not been declared
In file included from B.hpp:5,
                 from B.cpp:2:
A.hpp:10: error: ISO C++ forbids declaration of `B' with no type
A.hpp:10: error: expected `;' before '*' token
</code></pre>
<p>Warum? Und was kann ich dagegen tun?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/148328/alles-korrekt-trotzdem-seltsame-fehler</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Sep 2026 19:40:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/148328.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 May 2006 15:14:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Alles korrekt. Trotzdem seltsame Fehler. on Thu, 25 May 2006 15:14:20 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe hier das folgende kleine simple Beispielprogramm:</p>
<pre><code class="language-cpp">// A.cpp
#include &quot;A.hpp&quot;

void A::foo()
{
  b = new B();
  b-&gt;bar();
}

void A::bar()
{
}

// A.hpp
#ifndef __A_HPP__
#define __A_HPP__

#include &quot;B.cpp&quot;

class A
{
public:
  B *b
  void foo();
  void bar();
};

#endif

// B.cpp
#include &quot;B.hpp&quot;

void B::foo()
{
  a = new A();
  a-&gt;bar();
}

void B::bar()
{
}

// B.hpp
#ifndef __B_HPP__
#define __B_HPP__

#include &quot;A.hpp&quot;

class B
{
public:
  A *a;
  void foo();
  void bar();
};

#endif

// main.cpp
#include &quot;A.hpp&quot;
#include &quot;B.hpp&quot;

main()
{
  A a;
  a.foo();
  B b;
  b.foo();
}
</code></pre>
<p>Wenn ich das mit</p>
<pre><code>g++ main.cpp A.cpp B.cpp
</code></pre>
<p>kompileire bekomme ich die folgenden seltsamen Fehlermeldungen:</p>
<pre><code>In file included from B.cpp:2,
                 from A.hpp:5,
                 from main.cpp:2:
B.hpp:10: error: ISO C++ forbids declaration of `A' with no type
B.hpp:10: error: expected `;' before '*' token
In file included from A.hpp:5,
                 from main.cpp:2:
B.cpp: In member function `void B::foo()':
B.cpp:6: error: `a' was not declared in this scope
B.cpp:6: error: `A' has not been declared
In file included from main.cpp:2:
A.hpp: At global scope:
A.hpp:11: error: expected `;' before &quot;void&quot;
main.cpp: In function `int main()':
main.cpp:8: error: 'class A' has no member named 'foo'
In file included from B.cpp:2,
                 from A.hpp:5,
                 from A.cpp:2:
B.hpp:10: error: ISO C++ forbids declaration of `A' with no type
B.hpp:10: error: expected `;' before '*' token
In file included from A.hpp:5,
                 from A.cpp:2:
B.cpp: In member function `void B::foo()':
B.cpp:6: error: `a' was not declared in this scope
B.cpp:6: error: `A' has not been declared
In file included from A.cpp:2:
A.hpp: At global scope:
A.hpp:11: error: expected `;' before &quot;void&quot;
A.cpp:5: error: no `void A::foo()' member function declared in class `A'
A.cpp: In member function `void A::foo()':
A.cpp:6: error: `b' was not declared in this scope
A.cpp: At global scope:
A.cpp:11: error: no `void A::bar()' member function declared in class `A'
A.cpp:11: error: `void A::bar()' and `void A::bar()' cannot be overloaded
In file included from A.hpp:5,
                 from B.hpp:5,
                 from B.cpp:2:
B.cpp:4: error: `B' has not been declared
B.cpp: In function `void foo()':
B.cpp:6: error: `a' was not declared in this scope
B.cpp:6: error: `A' has not been declared
B.cpp: At global scope:
B.cpp:10: error: `B' has not been declared
In file included from B.hpp:5,
                 from B.cpp:2:
A.hpp:10: error: ISO C++ forbids declaration of `B' with no type
A.hpp:10: error: expected `;' before '*' token
</code></pre>
<p>Warum? Und was kann ich dagegen tun?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1064962</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1064962</guid><dc:creator><![CDATA[Programmiererin]]></dc:creator><pubDate>Thu, 25 May 2006 15:14:20 GMT</pubDate></item><item><title><![CDATA[Reply to Alles korrekt. Trotzdem seltsame Fehler. on Thu, 25 May 2006 15:37:04 GMT]]></title><description><![CDATA[<p>Wieso &quot;alles korrekt&quot;? Das ist sehr anmaßend gegenüber des Compilers, der weiß es in den allermeisten Fällen (so auch hier) besser.</p>
<p>1. __ Präfixe sind dem Compiler vorbehalten, also:</p>
<pre><code class="language-cpp">// A.hpp
#ifndef A_HPP
#define A_HPP
</code></pre>
<p>2. man inkludiert im allgemeinen keine Implementationsdatei, ist hier auch nicht sinnvoll</p>
<pre><code class="language-cpp">// #include &quot;B.cpp&quot;
#include &quot;B.hpp&quot;
</code></pre>
<p>3. Der Präprozessor ist dumm. Das ist eine allgemeingültige Aussage, so ähnlich wie &quot;Die Wirtschaft ist böse.&quot; Egal wie du es drehst, diese Zeile bringt dir nichts. B.hpp wird von A.hpp inkludiert (siehe main.cpp) <strong>bevor</strong> die Klasse A deklariert wurde. Das include hier verläuft schon in der ersten Zeile von A.hpp, da A_HPP bereits beim ersten inkludieren von A.hpp definiert wurde.<br />
Der einzige Ausweg (der hier auch ohne Probleme möglich ist, da a nur ein Zeiger auf A ist ist folgendes:</p>
<pre><code class="language-cpp">// #include &quot;A.hpp&quot;
class A;
</code></pre>
<p>Nun muss allerdings B.cpp zusätzlich zu B.hpp noch A.hpp inkludieren, aber das sollte man generell tun.</p>
<p>4. main hat keine impliziten int-Rückgabetyp, dieser muss angegeben werden</p>
<pre><code class="language-cpp">// main()
int main ()
</code></pre>
<p>4 &gt; 0 =&gt; !(alles korrekt) :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1064973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1064973</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Thu, 25 May 2006 15:37:04 GMT</pubDate></item><item><title><![CDATA[Reply to Alles korrekt. Trotzdem seltsame Fehler. on Thu, 25 May 2006 16:34:10 GMT]]></title><description><![CDATA[<p>zu 1:<br />
Wusste ich nicht. Danke.</p>
<p>zu 2:<br />
Tippfehler. Sowas mache ich normalerweise nicht (Außer bei templates).</p>
<p>zu 3:<br />
Danke, das löst das Problem.</p>
<p>zu 4:<br />
Hab ich in der Eile vergessen.</p>
<p>BTW: Was macht man denn bei so einem Problem:</p>
<pre><code class="language-cpp">// A.cpp
#include &quot;B.hpp&quot;
#include &quot;A.hpp&quot;

void A::foo()
{
  B* b = new B();
  b-&gt;bar = 42;
}

// A.hpp
#ifndef A_HPP
#define A_HPP

class B;

class A
{
private:
  int bar;
public:
  friend void B::foo();
  void foo();
};

#endif

// B.cpp
#include &quot;A.hpp&quot;
#include &quot;B.hpp&quot;

void B::foo()
{
  A* a = new A();
  a-&gt;bar = 42;
}

// B.hpp
#ifndef B_HPP
#define B_HPP

class A;

class B
{
private:
  int bar;
public:
  friend void A::foo();
  void foo();
};

#endif

// main.cpp
#include &quot;A.hpp&quot;
#include &quot;B.hpp&quot;

int main()
{
  A *a = new A();
  a-&gt;foo();
  B *b = new B();
  b-&gt;foo();
}
</code></pre>
<p>Der Compiler meint:</p>
<pre><code>In file included from main.cpp:2:
A.hpp:12: error: member `void B::foo()' declared as friend before type `B' defined
In file included from B.cpp:2:
A.hpp:12: error: member `void B::foo()' declared as friend before type `B' defined
B.cpp: In member function `void B::foo()':
A.hpp:10: error: `int A::bar' is private
B.cpp:8: error: within this context
In file included from A.cpp:2:
B.hpp:12: error: member `void A::foo()' declared as friend before type `A' defined
A.cpp: In member function `void A::foo()':
B.hpp:10: error: `int B::bar' is private
A.cpp:8: error: within this context
</code></pre>
<p>Gibt's hier auch so einen Trick, oder muss man sich gleich mit der ganzen Klasse anfreunden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1065008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1065008</guid><dc:creator><![CDATA[Programmiererin]]></dc:creator><pubDate>Thu, 25 May 2006 16:34:10 GMT</pubDate></item><item><title><![CDATA[Reply to Alles korrekt. Trotzdem seltsame Fehler. on Thu, 25 May 2006 16:40:24 GMT]]></title><description><![CDATA[<p>Du hast Spaß mit deinen zyklischen Abhängigkeiten, wa? <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 />
Naja, aber das neue Problem ist an sich nicht lösbar (zumindest sehe ich keine Lösung). B muss die gesamte Deklaration von A kennen, welches die gesamte Deklaration von B kennen muss. Das ist nicht möglich (um das nochmal zu betonen).<br />
Ein reales Problem dieser Art fußt praktisch immer auf einem miesen Klassendesign, also versuch gar nicht erst, weiter über eine Lösung nachzudenken <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1065012</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1065012</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Thu, 25 May 2006 16:40:24 GMT</pubDate></item><item><title><![CDATA[Reply to Alles korrekt. Trotzdem seltsame Fehler. on Thu, 25 May 2006 18:18:38 GMT]]></title><description><![CDATA[<p>Das einzige was stört ist die Tatsache, dass &quot;bar&quot; private ist und direkt verändert werden soll.... also mache einfach eine public-Funktion &quot;setBar&quot; in beiden Klassen oder mach &quot;bar&quot; gleich public und schmeiße die friend-Deklarationen raus!</p>
<p><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/1065070</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1065070</guid><dc:creator><![CDATA[toller_pseudo_name]]></dc:creator><pubDate>Thu, 25 May 2006 18:18:38 GMT</pubDate></item></channel></rss>