Probleme mit static variable in klasse
-
#include<iostream> using namespace std; class timer{ private: static int times ; public: void SetTimer() ; void TimerUp() ; int TimerRead() const; }; // ende klasse void timer:: SetTimer(){ cout << "Bitter Wert fuer Timer eingeben : " ; cin >> times ; } void timer:: TimerUp(){ times ++ ; } int timer:: TimerRead() const{ return times; } int main(){ timer timer1; timer timer2; timer timer3; timer1.SetTimer(); cout << "Timer 1 : " << timer1.TimerRead() << endl; cout << "Timer 2 : " << timer2.TimerRead() << endl; cout << "Timer 3 : " << timer3.TimerRead() << endl; timer1.TimerUp(); cout << "Timer 1 : " << timer1.TimerRead() << endl; cout << "Timer 2 : " << timer2.TimerRead() << endl; cout << "Timer 3 : " << timer3.TimerRead() << endl; return 0 ; } // ende mainich habe da ein kleines Programm das beim compilieren folgende Meldung ausgibt:
/tmp/cc8uCnEU.o(.text+0x1e): In functiontimer::SetTimer()': : undefined reference totimer::times'
/tmp/cc8uCnEU.o(.text+0x35): In functiontimer::TimerUp()': : undefined reference totimer::times'
/tmp/cc8uCnEU.o(.text+0x40): In functiontimer::TimerRead() const': : undefined reference totimer::times'
collect2: ld returned 1 exit statusWenn icht static weglasse dann geht alles , aber dann passt das Programm nicht mehr. Wie muss ich static verwenden dass ich das umsetzten kann was das Programm offentsichtlich machen soll .
-
Dir fehlt eine Definition von times, bis jetzt hast du times nur deklariert. Gab schon einige Threads dazu

http://www.c-plusplus.net/forum/viewtopic-var-t-is-39471.html
MfG SideWinder
-
int timer::times = 0;
-
Die Deklaration der Variablen in der Klasse ist genau das: Nur eine Deklaration, keine Definition. Es wird nur ein Name bekannt gemacht, aber kein Speicher für die Variable bereitgestellt. Das mußt Du zusätzlich tun, indem Du in Deine Implementierungsdatei noch ein
int timer::times;
reinschreibst.
MfG Jester
-
Hi und danke für eure schnelle Hilfe.
Hat jetzt wie nicht anderst erwatet geklappt .