Hier mache ich Standford C++ Kursarbeit.Stanford C++ Kursarbeit. Gauss 'Kindheit
Übung 03 aus Leserübungen. Hier ist die Frage gegeben:
Exercise 03 Compute the sum of the numbers between 1 and 100.
/* As mathematical historians have told the story, the German mathematician Karl Friedrich Gauss (1777-1855) began to show his mathematical talent at a very early age. When he was in elementary school, Gauss was asked by his teacher to compute the sum of the numbers between 1 and 100. Gauss is said to have given the answer instantly: 5050. Write a program that computes the answer to the question Gauss’s teacher posed. */
Der Fehler, den ich immer bin, wenn dieses Programm ausgeführt ist dies => Guassian.cpp: In der Funktion 'int main()': Guassian.cpp: 29: 12: error: ungültige Verwendung der nicht statischen Elementfunktion ob1.sumodds;
Was ist los?
#include<iostream>
using namespace std;
class Guassian
{
public:
int sumodds (int last)
{
int result = 0;
int odd = 1;
for (int i = 0; i < last; i++)
{
result += odd;
odd += 2;
}
return result ;
}
};
int main()
{
Guassian ob1;
ob1.sumodds;
//return 0;
}
gut ...... versuchen, Ihrer Funktion einen Wert zu geben könnte helfen – Mehno
'ob1.sumodds;' ist eine Member-Funktion und erfordert aufgerufen werden als 'ob1.sumodds (last);' was auch immer 'last' sollte sein. –
Programmieren Sie in C++ oder programmieren Sie Java in C++? Die C++ - Sprache erlaubt * freistehende * Funktionen. Sie müssen nicht alles in eine Klasse stecken. Zum Beispiel ist das "Haupt" eine freistehende Funktion. –