<?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[Include über Kreuz]]></title><description><![CDATA[<p>Hallo zusammen.</p>
<p>Ich weiß, das Thema wurde schon dutzende Male besprochen, aber nach mehreren Stunden suchen und probieren komme ich auf keinen grünen Zweig.</p>
<p>Ich habe eine Klasse &quot;threadClass&quot;, welche einen Thread kapselt. Eine andere Klasse &quot;caller&quot; hat je eine Instanz dieser threadClass. Die threadClass soll aber wiederum eine Methode der Klasse &quot;caller&quot; aufrufen. Damit habe ich das klassische Problem, dass ich die Files gegenseitig includen müsste. Ich habe es auch schon mit Vorwärtsdeklarationen probiert, vergebens. Der Compiler sagt dann &quot;Verwendung des undefinierten Types...&quot;</p>
<p>caller.h</p>
<pre><code class="language-cpp">#ifndef _CALLER_H_
#define _CALLER_H_

#include &lt;iostream&gt;

using namespace std;

class threadClass;

class caller
{
public:
	caller(int myNumber);
	~caller();
	void startThread();
	virtual void threadCallbackFunction(int num);

private:
	int n;
	threadClass* myThread;
};
#endif
</code></pre>
<p>caller.cpp</p>
<pre><code class="language-cpp">#include &quot;caller.h&quot;

caller::caller(int myNumber)
{
	n = myNumber;
}

caller::~caller()
{
	myThread-&gt;kill();
        // Ich weiß, hier muss ich noch sauber auf die Beendigung warten
	delete(myThread);
}

void caller::startThread()
{
	myThread = new threadClass();
}

void caller::threadCallbackFunction(int num)
{
	cout &lt;&lt; &quot;Hello from caller &quot; &lt;&lt; n &lt;&lt; &quot; (&quot; &lt;&lt; num &lt;&lt; &quot; times called)&quot; &lt;&lt;endl;
}
</code></pre>
<p>threadClass.h</p>
<pre><code class="language-cpp">#ifndef _THREADCLASS_H_
#define _THREADCLASS_H_

#include &lt;Windows.h&gt;
#include &quot;caller.h&quot;

class threadClass
{
public:
	threadClass(caller* classptr);
	~threadClass();

	void start();
	void kill();

	virtual DWORD ThreadFunction(void);

private:
	HANDLE	hThread;
	DWORD	dwThreadID;  
	LPVOID	pParam;
	bool	killThread;
	caller* cptr;
	int		execNumber;

	enum Status
	{
		STOPPED,
		RUNNING,
		SUSPENDING
	};

	int status;

protected:
	static DWORD WINAPI StaticThreadFunction(void* pthis);
};
#endif
</code></pre>
<p>threadClass.cpp</p>
<pre><code class="language-cpp">#include &quot;threadClass.h&quot;

threadClass::threadClass(caller* classptr)
{
	killThread	= false;
	cptr		= classptr;
	execNumber	= 0;
}

threadClass::~threadClass()
{}

void threadClass::start()
{
	if (status == RUNNING) 
	{
		// Thread is running 
		return;
	}
	else if (status == SUSPENDING)
	{
		// Thread continue
		status = RUNNING;
		ResumeThread(hThread);
	}
	else
	{
		// Thread start
		status = RUNNING;
		hThread = CreateThread(
						NULL, 
						0, 
						StaticThreadFunction, 
						this, 
						0, 
						&amp;dwThreadID);
	}
}

void threadClass::kill()
{
	killThread = true;
}

DWORD threadClass::ThreadFunction(void)
{
	while (!killThread)
	{
		execNumber++;
		cptr-&gt;threadCallbackFunction(execNumber);
	}
	return 0;
}

DWORD WINAPI threadClass::StaticThreadFunction(void* pthis)
{
	threadClass *pThis;

	pThis = (threadClass *) pthis;
	// Ich weiß. Ist ein C cast. 

	return pThis-&gt;ThreadFunction();
}
</code></pre>
<p>Wie muss ich das strukturieren, damit es funktioniert?</p>
<p>Ich habe auch schon mehrfach die Meinung gehört, dass das schlechtes Design ist. Allerdings wüsste ich nicht, wie ich es anderes lösen soll.<br />
Ich möchte die Thread-Geschichte in einer separaten Klasse haben, damit ich sie auch nach dem gleichen Mechanismus für andere Aufrufer-Klassen einsetzen kann (dann durch Vererbung oder Templates).</p>
<p>Grüße,<br />
Ziu</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/270629/include-über-kreuz</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 00:13:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/270629.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 Jul 2010 13:42:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 13:42:25 GMT]]></title><description><![CDATA[<p>Hallo zusammen.</p>
<p>Ich weiß, das Thema wurde schon dutzende Male besprochen, aber nach mehreren Stunden suchen und probieren komme ich auf keinen grünen Zweig.</p>
<p>Ich habe eine Klasse &quot;threadClass&quot;, welche einen Thread kapselt. Eine andere Klasse &quot;caller&quot; hat je eine Instanz dieser threadClass. Die threadClass soll aber wiederum eine Methode der Klasse &quot;caller&quot; aufrufen. Damit habe ich das klassische Problem, dass ich die Files gegenseitig includen müsste. Ich habe es auch schon mit Vorwärtsdeklarationen probiert, vergebens. Der Compiler sagt dann &quot;Verwendung des undefinierten Types...&quot;</p>
<p>caller.h</p>
<pre><code class="language-cpp">#ifndef _CALLER_H_
#define _CALLER_H_

#include &lt;iostream&gt;

using namespace std;

class threadClass;

class caller
{
public:
	caller(int myNumber);
	~caller();
	void startThread();
	virtual void threadCallbackFunction(int num);

private:
	int n;
	threadClass* myThread;
};
#endif
</code></pre>
<p>caller.cpp</p>
<pre><code class="language-cpp">#include &quot;caller.h&quot;

caller::caller(int myNumber)
{
	n = myNumber;
}

caller::~caller()
{
	myThread-&gt;kill();
        // Ich weiß, hier muss ich noch sauber auf die Beendigung warten
	delete(myThread);
}

void caller::startThread()
{
	myThread = new threadClass();
}

void caller::threadCallbackFunction(int num)
{
	cout &lt;&lt; &quot;Hello from caller &quot; &lt;&lt; n &lt;&lt; &quot; (&quot; &lt;&lt; num &lt;&lt; &quot; times called)&quot; &lt;&lt;endl;
}
</code></pre>
<p>threadClass.h</p>
<pre><code class="language-cpp">#ifndef _THREADCLASS_H_
#define _THREADCLASS_H_

#include &lt;Windows.h&gt;
#include &quot;caller.h&quot;

class threadClass
{
public:
	threadClass(caller* classptr);
	~threadClass();

	void start();
	void kill();

	virtual DWORD ThreadFunction(void);

private:
	HANDLE	hThread;
	DWORD	dwThreadID;  
	LPVOID	pParam;
	bool	killThread;
	caller* cptr;
	int		execNumber;

	enum Status
	{
		STOPPED,
		RUNNING,
		SUSPENDING
	};

	int status;

protected:
	static DWORD WINAPI StaticThreadFunction(void* pthis);
};
#endif
</code></pre>
<p>threadClass.cpp</p>
<pre><code class="language-cpp">#include &quot;threadClass.h&quot;

threadClass::threadClass(caller* classptr)
{
	killThread	= false;
	cptr		= classptr;
	execNumber	= 0;
}

threadClass::~threadClass()
{}

void threadClass::start()
{
	if (status == RUNNING) 
	{
		// Thread is running 
		return;
	}
	else if (status == SUSPENDING)
	{
		// Thread continue
		status = RUNNING;
		ResumeThread(hThread);
	}
	else
	{
		// Thread start
		status = RUNNING;
		hThread = CreateThread(
						NULL, 
						0, 
						StaticThreadFunction, 
						this, 
						0, 
						&amp;dwThreadID);
	}
}

void threadClass::kill()
{
	killThread = true;
}

DWORD threadClass::ThreadFunction(void)
{
	while (!killThread)
	{
		execNumber++;
		cptr-&gt;threadCallbackFunction(execNumber);
	}
	return 0;
}

DWORD WINAPI threadClass::StaticThreadFunction(void* pthis)
{
	threadClass *pThis;

	pThis = (threadClass *) pthis;
	// Ich weiß. Ist ein C cast. 

	return pThis-&gt;ThreadFunction();
}
</code></pre>
<p>Wie muss ich das strukturieren, damit es funktioniert?</p>
<p>Ich habe auch schon mehrfach die Meinung gehört, dass das schlechtes Design ist. Allerdings wüsste ich nicht, wie ich es anderes lösen soll.<br />
Ich möchte die Thread-Geschichte in einer separaten Klasse haben, damit ich sie auch nach dem gleichen Mechanismus für andere Aufrufer-Klassen einsetzen kann (dann durch Vererbung oder Templates).</p>
<p>Grüße,<br />
Ziu</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926694</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926694</guid><dc:creator><![CDATA[DarthZiu]]></dc:creator><pubDate>Thu, 15 Jul 2010 13:42:25 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 13:48:58 GMT]]></title><description><![CDATA[<p>Also bei einer Vorwärtsdeklartaion musst du trotzdem das file dann in der cpp includieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926700</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926700</guid><dc:creator><![CDATA[y-vonne]]></dc:creator><pubDate>Thu, 15 Jul 2010 13:48:58 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 14:06:29 GMT]]></title><description><![CDATA[<p>Haut trotzdem nicht hin.</p>
<p>Dann kennt der Compiler die Klasse &quot;caller&quot; nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926710</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926710</guid><dc:creator><![CDATA[DarthZiu]]></dc:creator><pubDate>Thu, 15 Jul 2010 14:06:29 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 14:12:00 GMT]]></title><description><![CDATA[<p>und was sagt der compiler und in welcher Zeile?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926713</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926713</guid><dc:creator><![CDATA[y-vonne]]></dc:creator><pubDate>Thu, 15 Jul 2010 14:12:00 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 14:18:00 GMT]]></title><description><![CDATA[<p>Im cpp musst du natürlich dann den Header einbinden.<br />
Also in caller.cpp muss threadClass.h inkludiert werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926717</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926717</guid><dc:creator><![CDATA[Jockelx]]></dc:creator><pubDate>Thu, 15 Jul 2010 14:18:00 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 14:19:26 GMT]]></title><description><![CDATA[<p>Hei du Jockel lies mal 4 Beiträge weiter oben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926720</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926720</guid><dc:creator><![CDATA[ojeeee]]></dc:creator><pubDate>Thu, 15 Jul 2010 14:19:26 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 14:24:13 GMT]]></title><description><![CDATA[<p>Ja, wäre wohl schlauer gewesen <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/1926725</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926725</guid><dc:creator><![CDATA[Jockelx]]></dc:creator><pubDate>Thu, 15 Jul 2010 14:24:13 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 14:25:31 GMT]]></title><description><![CDATA[<p>Also ich hab jetzt:</p>
<p>caller.h<br />
#include &quot;threadClass.h&quot;<br />
Vorwärtsdeklaration: class threadClass;</p>
<p>caller.cpp<br />
#include &quot;caller.h&quot;<br />
#include &quot;threadClass.h&quot;</p>
<p>threadClass.h<br />
#include &quot;caller.h&quot;</p>
<p>threadClass.cpp<br />
#include &quot;threadClass.h&quot;<br />
#include &quot;caller.h&quot;</p>
<p>Ergebnis:</p>
<blockquote>
<p>1&gt;Kompilieren...<br />
1&gt;caller.cpp<br />
1&gt;...\threadstandalone\threadclass.h(10) : error C2061: Syntaxfehler: Bezeichner 'caller'<br />
1&gt;...\threadstandalone\threadclass.h(23) : error C2143: Syntaxfehler: Es fehlt ';' vor '*'<br />
1&gt;...\threadstandalone\threadclass.h(23) : error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: &quot;default-int&quot; wird von C++ nicht unterstützt.<br />
1&gt;...\threadstandalone\threadclass.h(23) : error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: &quot;default-int&quot; wird von C++ nicht unterstützt.<br />
1&gt;threadClass.cpp<br />
1&gt;Code wird generiert...<br />
1&gt;threadStandalone - 4 Fehler, 0 Warnung(en)<br />
========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1926727</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926727</guid><dc:creator><![CDATA[DarthZiu]]></dc:creator><pubDate>Thu, 15 Jul 2010 14:25:31 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 14:29:14 GMT]]></title><description><![CDATA[<p>hey,</p>
<p>Wenn du threadClass als vorwärtsdekleration hast, darfst/musst du es nicht mehr includieren in der h-Datei.<br />
Wenn du caller in der threadClass.h includierst, brauchst du es nicht mehr in der threadClass.cpp zu includiern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926730</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926730</guid><dc:creator><![CDATA[uwerothfeld]]></dc:creator><pubDate>Thu, 15 Jul 2010 14:29:14 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 14:40:37 GMT]]></title><description><![CDATA[<p>So sollte es gehen:</p>
<p>in threadclass.h:<br />
class caller; // Forward declaration</p>
<p>in caller.h<br />
class threadclass; // Forward declaration</p>
<p>in threadclass.cpp<br />
#include &quot;caller.h&quot;</p>
<p>in caller.cpp<br />
#include &quot;threadclass.h&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926741</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926741</guid><dc:creator><![CDATA[Source2702]]></dc:creator><pubDate>Thu, 15 Jul 2010 14:40:37 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Thu, 15 Jul 2010 15:23:15 GMT]]></title><description><![CDATA[<p>Ich verweis immer gerne hier hin:<br />
<a href="http://www.drakon.ch/?id=&amp;offset=5&amp;mobile=0&amp;show_entry=77" rel="nofollow">Definitionen und Deklarationen in C++</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1926772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1926772</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 15 Jul 2010 15:23:15 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Fri, 16 Jul 2010 08:24:37 GMT]]></title><description><![CDATA[<p>Source2702 schrieb:</p>
<blockquote>
<p>So sollte es gehen:</p>
<p>in threadclass.h:<br />
class caller; // Forward declaration</p>
<p>in caller.h<br />
class threadclass; // Forward declaration</p>
<p>in threadclass.cpp<br />
#include &quot;caller.h&quot;</p>
<p>in caller.cpp<br />
#include &quot;threadclass.h&quot;</p>
</blockquote>
<p>Nein, geht auch nicht...</p>
<blockquote>
<p>1&gt;------ Erstellen gestartet: Projekt: threadStandalone, Konfiguration: Debug Win32 ------<br />
1&gt;Kompilieren...<br />
1&gt;caller.cpp<br />
1&gt;...\threadstandalone\threadstandalone\caller.cpp(3) : error C2027: Verwendung des undefinierten Typs &quot;caller&quot;<br />
1&gt; ...\threadstandalone\threadclass.h(6): Siehe Deklaration von 'caller'<br />
1&gt;...\threadstandalone\caller.cpp(3) : error C2062: 'int'-Typ unerwartet<br />
1&gt;...\threadstandalone\caller.cpp(4) : error C2143: Syntaxfehler: Es fehlt ';' vor '{'<br />
1&gt;...\threadstandalone\caller.cpp(4) : error C2447: '{': Funktionsheader fehlt - Parameterliste im alten Stil?<br />
1&gt;...\threadstandalone\caller.cpp(8) : error C2027: Verwendung des undefinierten Typs &quot;caller&quot;</p>
<p>...</p>
<p>1&gt;Code wird generiert...<br />
1&gt;Kompilieren...<br />
1&gt;main.cpp<br />
1&gt;Code wird generiert...<br />
1&gt;threadStandalone - 61 Fehler, 1 Warnung(en)<br />
========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1927041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927041</guid><dc:creator><![CDATA[DarthZiu]]></dc:creator><pubDate>Fri, 16 Jul 2010 08:24:37 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Fri, 16 Jul 2010 08:28:42 GMT]]></title><description><![CDATA[<p>caller.h hast aber in caller.cpp schon inkludiert, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927044</guid><dc:creator><![CDATA[Source2702]]></dc:creator><pubDate>Fri, 16 Jul 2010 08:28:42 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Fri, 16 Jul 2010 08:29:24 GMT]]></title><description><![CDATA[<p>Wollte ich gerade schreiben, da lag der letzte Fehler.</p>
<p>Jetzt klappt's, danke sehr.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927046</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927046</guid><dc:creator><![CDATA[DarthZiu]]></dc:creator><pubDate>Fri, 16 Jul 2010 08:29:24 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Fri, 16 Jul 2010 08:32:02 GMT]]></title><description><![CDATA[<p>So, nochmal für alle, die diesen Thread in der Zukunft besuchen und die endgültige Lösung wissen wollen:</p>
<pre><code class="language-cpp">// threadClass.h
class caller;

// caller.h
class threadClass;

// threadClass.cpp
#include &quot;caller.h&quot;
#include &quot;threadClass.h&quot;

// caller.cpp
#include &quot;threadClass.h&quot;
#include &quot;caller.h&quot;
</code></pre>
<p>So läuft's bei mir jedenfalls, mit MS Visual Studio 2008.</p>
<p>Nochmal vielen Dank an alle Helfer!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927048</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927048</guid><dc:creator><![CDATA[DarthZiu]]></dc:creator><pubDate>Fri, 16 Jul 2010 08:32:02 GMT</pubDate></item><item><title><![CDATA[Reply to Include über Kreuz on Fri, 16 Jul 2010 15:21:14 GMT]]></title><description><![CDATA[<p>Äusserst merkwürdiges Konstrukt. Lies doch meinen verlinkten Artikel durch und probier das erneut. Es kann nicht sein, dass man von aussen solche Vorwärtsdeklarationen machen muss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927299</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927299</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Fri, 16 Jul 2010 15:21:14 GMT</pubDate></item></channel></rss>