C++ Klassenproblem!

F

freelogin

Gast
Hi Leute!
Ich sehe in meinem Bsp den Fehler (Ausgabe geht nicht richtig) nicht, vielleicht ist ja einer von euch ein Profi?!

Danke!

statistik.h
PHP:
#ifndef _STATISTIK_h
#define _STATISTIK_h
#include <stdlib.h>

class Statistik{
      private:
              double werte[10];
              int anzahl;
      public:
             Statistik(void);
             void print(void);
      
      
      
      };

#endif


statistik.cpp
PHP:
#include "statistik.h"
#include <cstdlib>
#include <iostream>

using namespace std;

//Konstruktoren:

Statistik::Statistik(void){
                           double werte[]={3,7,5,8,1};
                           int anzahl = 5;
                           }
                                
//Operationen

void Statistik::print(void){
                            int i;
                            for(i = 0; i < anzahl; i++){
                                  cout << werte[i] << " ";
                                  }
                            }


main.cpp
PHP:
#include <cstdlib>
#include <iostream>
#include "statistik.h"

using namespace std;

int main() {
    
    Statistik stati1;
    
    stati1.print();
    
    
    
    system("PAUSE");
    return 0;
}
 
Das geht nicht weil du im Konstruktor anzahl noch einmal deklarierst! Somit legst du eine neue Variable anzahl an, die nicht anzahl im äußersten Sichtbarkeitsbereich entspricht.
Du setzt damit nur das innere anzahl auf 5, nicht das das du später auch verwendest, das bleibt 0 und somit läuft die Schleife auch nie durch.
 
ah, Danke!

Hmm, hast Du eine Idee, wie ich trotzdem ein Arrey des Typs Statistik anlegen , an dem ich meine ganzen Operationen (Ausgabe, größter Wert, kleinster Wert, Durchschnitt, etc) ausprobieren kann?

Merci!
 
Habs gefunden :)

die neue statistik.cpp:

PHP:
#include "statistik.h"
//#include <cstdlib>
#include <iostream>

using namespace std;

//Konstruktoren:

Statistik::Statistik(void){
                           stat_werte[0]= 3;
                           stat_werte[1]= 7;
                           stat_werte[2]= 5;
                           stat_werte[3]= 1;
                           stat_werte[4]= 4;
                           
                           stat_anzahl = 5;
                           }
                                
//Operationen

void Statistik::print(void){
                            int i;
                            for(i = 0; i < stat_anzahl; i++){
                                  cout << stat_werte[i] << " ";
                                  }
                            cout << endl;
                            }
 
Zurück
Oben