C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Klassen Problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Fabian Knopf
Guest





PostPosted: Thu Oct 30, 2003 7:16 pm    Post subject: Klassen Problem Reply with quote



{Your article uses the wrong language (German rather than English) and is
thus rejected as off-topic from comp.lang.c++.moderated. Please direct
your article to an appropriate forum, eg. de.comp.lang.iso-c++. Thank
you. -mod}

Hi Freunde des C++!

Habe hier ein wahrscheinlich billiges Problem aber ich versteh nicht warum
es nicht geht.

Habe eine Klasse und ein Main Projekt wo die main Funktion enthalten ist.

Sieht alles so aus....

Klasse--->

#include <iostream>


class Bruch{

private:

int m_zaehler;
int m_nenner;

public:

int m_ergebnis;
int m_ausgabe;

Bruch();

int Berechnen(int m_zaehler,int m_nenner)
{
m_ergebnis = m_zaehler / m_nenner;
return m_ergebnis;
}

void Print()
{
std::cout<
}

~Bruch();
};


Dann die Main----->

#include <iostream>
#include "Klassen.cpp"



void main(void)
{

Bruch::Berechnen(10,5);

}


Wo liegt mein Problem in der Main wird die Klasse erkannt aber die
compilierung meint
--> Unzulaessiger Aufruf einer nichtstatischen Member-Funktion

Bitte um Hilfe!

MFG,

Fabse

P.S. Danke schon mal im vorraus für lesen und evtl. beantwortung!!!



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ron Natalie
Guest





PostPosted: Fri Oct 31, 2003 10:11 am    Post subject: Re: Klassen Problem Reply with quote




"Fabian Knopf" <F.Knopf (AT) gmx (DOT) de> wrote

Quote:
{Your article uses the wrong language (German rather than English) and is
thus rejected as off-topic from comp.lang.c++.moderated. Please direct
your article to an appropriate forum, eg. de.comp.lang.iso-c++. Thank
you. -mod}

Gets pretty wide distribution for a rejected posting :-)

{ The finger is faster than the brain sometimes. I guess English
followups can be accepted. -mod/jep }

Anyway, the poster wants to know why his program doesn't work:
Unzulaessiger Aufruf einer nichtstatischen Member-Funktion

which is something like
Illegal call of non-member function

Quote:
void main(void)

Main must return int (not the problem.

Quote:
{

Bruch::Berechnen(10,5);

}

A non-static member function must be called in the context of the object. Since
Berechnen changes an non-static data member, it's clear it needs an object (as
opposed to being misdefined as non-static).

int main() {
Bruch b;
b.Berechnen(10, 5);
}

Excuse the machine translation:
Eine nicht-statische Mitgliedsfunktion muß im Kontext des Gegenstandes benannt werden.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Klaus Eichner
Guest





PostPosted: Fri Oct 31, 2003 10:12 am    Post subject: Re: Klassen Problem Reply with quote



"Fabian Knopf" <F.Knopf (AT) gmx (DOT) de> wrote


[Snip]

Quote:
#include class Bruch{
private:
int m_zaehler;
int m_nenner;
public:
int m_ergebnis;
int m_ausgabe;
Bruch();
int Berechnen(int m_zaehler,int m_nenner)
{
m_ergebnis = m_zaehler / m_nenner;
return m_ergebnis;
}

[Snip]

Quote:
void main(void)
{
Bruch::Berechnen(10,5);
}

I found two problems with the code above:
problem 1: it uses 'void main(void)'
problem 2: it tries to call a non-static member function 'Bruch::Berechnen'
without any object.

You should write instead:

int main(void)
{
Bruch b;
b.Berechnen(10,5);
}

[Snip]




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Arnaud Troel
Guest





PostPosted: Fri Oct 31, 2003 10:12 am    Post subject: Re: Klassen Problem Reply with quote

Fabian Knopf wrote:
[snip]
Quote:
class Bruch{
[snip]
int Berechnen(int m_zaehler,int m_nenner)
[snip]

void main(void)
{
Bruch::Berechnen(10,5);
}



Your function Berechnen is not a static. Therefore you must have an
object to call it, e.g. "Bruch b; b.Berechnen(10,5);".

If you want Berechnen to be a class method, declare it as a static one:

static int Berechnen(...)


(Deine funktion ist nicht "static". Du muss ein objekt haben um sie zu
rennen.)


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Peter Schneider
Guest





PostPosted: Sun Nov 16, 2003 11:24 am    Post subject: Re: Klassen Problem Reply with quote

Hi,

Fabian Knopf wrote

Quote:
#include <iostream


class Bruch{

private:

int m_zaehler;
int m_nenner;

public:

int m_ergebnis;
int m_ausgabe;

Bruch();

int Berechnen(int m_zaehler,int m_nenner)
{
m_ergebnis = m_zaehler / m_nenner;
return m_ergebnis;
}

void Print()
{
std::cout<
}

~Bruch();
};

Habe hier ein wahrscheinlich billiges Problem
[I have probably a cheap little problem]


Yes and no.
I think that the syntactic error which the others pointed out is a symptom of a
semantic problem regarding your class/application design.

Obviously, Berechnen() does not need any information of a Bruch object. The
information needed is provided through the parameters. Therefore it could be a
standalone function, a function in a namespace or a static class function. But the
function has side effects, it sets the value of members of the object; in a sense,
it's a combination of a constructor which sets data members and a parameterless
method which uses the data members that have been set in the constructor. That can be
a valid design for convenience, but without a certain reason I'd rather suggest to
have it separated.

I admit that it can be convenient to have a static function that computes the
fraction of two numbers; in a more complicated class it could use algorithms coded in
helper functions that are known to or members of the class. for example, in Java the
class Integer provides a "static int parseInt(String)" function which does not create
any Integer object but uses "knowledge" found in the Integer class, or at least fits
nicely in the environment.

Last not least, I also think that your m_ergebnis is redundant. Usually the only
thing for which you'd need m_ergebnis is printing; all other computation with
fractional numbers you'll do with nenner and zaehler. Printing however is so time
consuming that you can compute the result on the fly without performance issues. If
there are no other reasons for keeping the redundant information, it's probably best
to avoid it.

So, my suggestion would look like

#include using namespace std;

class Bruch
{ int m_zaehler; int m_nenner;
public:
Bruch(int zaehler, int nenner)
: m_zaehler(zaehler),
m_nenner(nenner)
{}

/** No parameters, uses members */
int Berechnen() { return m_zaehler/m_nenner; }

/** @return zaehler/nenner */
static int Berechnen( int zaehler, int nenner)
{ return zaehler/nenner;
}
// ...
};

int main()
{
Bruch b(8,2);
cout << b.Berechnen() << endl;
// do more with B

// or if you don't need the object later
cout << Bruch(8,2).Berechnen() << endl;

// or better with the new static function
cout << Bruch::Berechnen(8,2) << endl;
}

Hope it helps,
Peter




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.