<?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[Funktionspointer auf Klasse zur Berechnung eines Integrals]]></title><description><![CDATA[<p>hallo erstmal.<br />
Ich habe die Aufgabe eine klasse zu schreiben die die Integration einer einfachen Funktion vornimmt bei der die Integrationsgrenzen und die Anzahl der Iterationsschritte private sind und somit mit get und set routinen eingelesen werden müssen. War ja alles schön und gut nur plagt mich der Funktionspointer der im selben Beispiel ohne Klassenaufteilung wunderbar funktioniert.</p>
<p>Erstmal die Klassendeklaration mit ein bisschen inline definition :</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;

class integral
{
public:
    integral() {lower=1; upper=10; step=100;}       //constructor of objects of the class integral
	~integral(){}                                   //destructor of objects of the class integral

	double f(double x){return sqrt(5*x-1);}

	double trapezium(double (*f)(double),double,double,int);
	double rectangle(double (*f)(double),double,double,int);

    double GetLower()  {return lower;}              //inline defined get Routines
    double GetUpper()  {return upper;}
	int GetStep()      {return step;}

    void SetLower(double p)   {lower=p;}            //inline defined set routines
    void SetUpper(double p)   {upper=p;}
	void SetStep(int p)       {step=p;}

    void ShowResultTrapezium();
	void ShowResultRectangle();

private:
    double lower;
    double upper;
	int step;
};
</code></pre>
<p>Hier das File der Klassendefinition :</p>
<pre><code class="language-cpp">#include&quot;integralclassheader.h&quot;
#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;

void integral::ShowResultTrapezium()
{
	cout &lt;&lt;endl&lt;&lt;&quot;Approximated integral value (trapezium rule) : &quot;&lt;&lt;integral::trapezium(f,lower, upper, step)&lt;&lt; endl;
}

void integral::ShowResultRectangle()
{
	cout &lt;&lt;endl&lt;&lt;&quot;Approximated integral value (rectangle rule) : &quot;&lt;&lt;integral::rectangle(f,lower, upper, step)&lt;&lt; endl;
}

double integral::trapezium(double(*f)(double), double lower, double upper, int step)
{	
	double xi,h,result=0;

	h=(upper-lower)/step;

	for(int i=1;i&lt;step;i++)
	{
		xi=lower+i*h;
		result=result+f(xi);
	}
	result=(f(lower)/2 + result + f(upper)/2)*h;

	return result;
}

double integral::rectangle(double(*f)(double), double lower,double upper,int step)
{
	double h,result=0;

	h=(upper-lower)/step;

	for(int i=1;i&lt;=step;i++)
	{
		result=result+f(lower+(i-(1./2))*h);         
	}
	result=result*h;

	return result;
}
</code></pre>
<p>und zuletzt die main :</p>
<pre><code class="language-cpp">#include&quot;integralclassheader.h&quot;
#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;

int main()
{
    integral integralobject;     
    double inLower, inUpper;
	int inStep;

	cout&lt;&lt;&quot;This program calculates the integral of a function with the trapezium and &quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;the rectangle rule and prints the results to stout. All operators &quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;and methods are stored in the class integral. By entering the values &quot;&lt;&lt;endl; 
	cout&lt;&lt;&quot;of a,b and n ,the constructor defines an object we can work with.&quot;&lt;&lt;endl&lt;&lt;endl;
	cout&lt;&lt;&quot;--------------------------------------------------------------------&quot;&lt;&lt;endl&lt;&lt;endl;

	cout &lt;&lt; &quot;Enter lower integral border a : &quot;;
    cin &gt;&gt;  inLower;
	integralobject.SetLower(inLower);

	cout &lt;&lt; &quot;Enter upper integral border b : &quot;;
    cin &gt;&gt;  inUpper;
	integralobject.SetUpper(inUpper);

	cout &lt;&lt; &quot;Enter number of iteration steps n : &quot;;
    cin &gt;&gt;  inStep;
    integralobject.SetStep(inStep);

	cout&lt;&lt;endl&lt;&lt;&quot;--------------------------------------------------------------------&quot;&lt;&lt;endl;

    integralobject.ShowResultTrapezium();
    integralobject.ShowResultRectangle();

	cout&lt;&lt;endl;
	system(&quot;pause&quot;);
	return 0;
}
</code></pre>
<p>Mein Problem ist jetzt dass alles was früher mit dem Funktionszeiger so toll geklappt hat nicht mehr funktioniert.</p>
<p>im Aufruf der Funktion in der Show - Methode schlägt mir der Compiler folgende Syntax vor :</p>
<p>Und zwar &amp;integral::f zu schreiben statt nur f. Doch dann häufen sich die Fehlermeldungen ins uferlose. Von der Ausprogrammierung der numerischen Integration her läuft das Programm ohne Probleme da ich diese Teile in Extraprogrammen gesondert untersucht habe. Also bleibt nur die Frage des Funktionzeigers.<br />
Kann mir vielleicht jemand helfen ?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/238185/funktionspointer-auf-klasse-zur-berechnung-eines-integrals</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 03:53:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/238185.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 07 Apr 2009 15:33:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Funktionspointer auf Klasse zur Berechnung eines Integrals on Tue, 07 Apr 2009 15:34:50 GMT]]></title><description><![CDATA[<p>hallo erstmal.<br />
Ich habe die Aufgabe eine klasse zu schreiben die die Integration einer einfachen Funktion vornimmt bei der die Integrationsgrenzen und die Anzahl der Iterationsschritte private sind und somit mit get und set routinen eingelesen werden müssen. War ja alles schön und gut nur plagt mich der Funktionspointer der im selben Beispiel ohne Klassenaufteilung wunderbar funktioniert.</p>
<p>Erstmal die Klassendeklaration mit ein bisschen inline definition :</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;

class integral
{
public:
    integral() {lower=1; upper=10; step=100;}       //constructor of objects of the class integral
	~integral(){}                                   //destructor of objects of the class integral

	double f(double x){return sqrt(5*x-1);}

	double trapezium(double (*f)(double),double,double,int);
	double rectangle(double (*f)(double),double,double,int);

    double GetLower()  {return lower;}              //inline defined get Routines
    double GetUpper()  {return upper;}
	int GetStep()      {return step;}

    void SetLower(double p)   {lower=p;}            //inline defined set routines
    void SetUpper(double p)   {upper=p;}
	void SetStep(int p)       {step=p;}

    void ShowResultTrapezium();
	void ShowResultRectangle();

private:
    double lower;
    double upper;
	int step;
};
</code></pre>
<p>Hier das File der Klassendefinition :</p>
<pre><code class="language-cpp">#include&quot;integralclassheader.h&quot;
#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;

void integral::ShowResultTrapezium()
{
	cout &lt;&lt;endl&lt;&lt;&quot;Approximated integral value (trapezium rule) : &quot;&lt;&lt;integral::trapezium(f,lower, upper, step)&lt;&lt; endl;
}

void integral::ShowResultRectangle()
{
	cout &lt;&lt;endl&lt;&lt;&quot;Approximated integral value (rectangle rule) : &quot;&lt;&lt;integral::rectangle(f,lower, upper, step)&lt;&lt; endl;
}

double integral::trapezium(double(*f)(double), double lower, double upper, int step)
{	
	double xi,h,result=0;

	h=(upper-lower)/step;

	for(int i=1;i&lt;step;i++)
	{
		xi=lower+i*h;
		result=result+f(xi);
	}
	result=(f(lower)/2 + result + f(upper)/2)*h;

	return result;
}

double integral::rectangle(double(*f)(double), double lower,double upper,int step)
{
	double h,result=0;

	h=(upper-lower)/step;

	for(int i=1;i&lt;=step;i++)
	{
		result=result+f(lower+(i-(1./2))*h);         
	}
	result=result*h;

	return result;
}
</code></pre>
<p>und zuletzt die main :</p>
<pre><code class="language-cpp">#include&quot;integralclassheader.h&quot;
#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;

int main()
{
    integral integralobject;     
    double inLower, inUpper;
	int inStep;

	cout&lt;&lt;&quot;This program calculates the integral of a function with the trapezium and &quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;the rectangle rule and prints the results to stout. All operators &quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;and methods are stored in the class integral. By entering the values &quot;&lt;&lt;endl; 
	cout&lt;&lt;&quot;of a,b and n ,the constructor defines an object we can work with.&quot;&lt;&lt;endl&lt;&lt;endl;
	cout&lt;&lt;&quot;--------------------------------------------------------------------&quot;&lt;&lt;endl&lt;&lt;endl;

	cout &lt;&lt; &quot;Enter lower integral border a : &quot;;
    cin &gt;&gt;  inLower;
	integralobject.SetLower(inLower);

	cout &lt;&lt; &quot;Enter upper integral border b : &quot;;
    cin &gt;&gt;  inUpper;
	integralobject.SetUpper(inUpper);

	cout &lt;&lt; &quot;Enter number of iteration steps n : &quot;;
    cin &gt;&gt;  inStep;
    integralobject.SetStep(inStep);

	cout&lt;&lt;endl&lt;&lt;&quot;--------------------------------------------------------------------&quot;&lt;&lt;endl;

    integralobject.ShowResultTrapezium();
    integralobject.ShowResultRectangle();

	cout&lt;&lt;endl;
	system(&quot;pause&quot;);
	return 0;
}
</code></pre>
<p>Mein Problem ist jetzt dass alles was früher mit dem Funktionszeiger so toll geklappt hat nicht mehr funktioniert.</p>
<p>im Aufruf der Funktion in der Show - Methode schlägt mir der Compiler folgende Syntax vor :</p>
<p>Und zwar &amp;integral::f zu schreiben statt nur f. Doch dann häufen sich die Fehlermeldungen ins uferlose. Von der Ausprogrammierung der numerischen Integration her läuft das Programm ohne Probleme da ich diese Teile in Extraprogrammen gesondert untersucht habe. Also bleibt nur die Frage des Funktionzeigers.<br />
Kann mir vielleicht jemand helfen ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1692411</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1692411</guid><dc:creator><![CDATA[Rachanol]]></dc:creator><pubDate>Tue, 07 Apr 2009 15:34:50 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionspointer auf Klasse zur Berechnung eines Integrals on Tue, 07 Apr 2009 15:54:37 GMT]]></title><description><![CDATA[<p>eine nichtstatische memberfunktion kann nur mit einem this-pointer aufgerufen werden.<br />
schreib mal ein static vor die deklaration von f().</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1692424</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1692424</guid><dc:creator><![CDATA[nott städig]]></dc:creator><pubDate>Tue, 07 Apr 2009 15:54:37 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionspointer auf Klasse zur Berechnung eines Integrals on Tue, 07 Apr 2009 17:55:54 GMT]]></title><description><![CDATA[<p>Vielen dank. Jetzt funktioniert das Programm wie es soll.<br />
Ich hab das jetzt mal so geschrieben dass ich ohne Probleme auf die Funktion f zugreifen kann. Ich werde nun versuchen es ohne static und mit this -&gt; zu arbeiten. Wenns nicht funktionieren will melde ich mich nochmal <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/1692484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1692484</guid><dc:creator><![CDATA[Rachanol]]></dc:creator><pubDate>Tue, 07 Apr 2009 17:55:54 GMT</pubDate></item></channel></rss>