2016-04-07 8 views
0

Ich stolperte über diesen Fehler beim Kompilieren meiner C++ - Codierung. hier ist mein Code:undefined Verweis auf `SavingsAccount :: annualInterestRate '

#include<iostream> 
#include<iomanip> 

using namespace std; 

class SavingsAccount 
{ 
private: 
    static float annualInterestRate; 
    float savingBalance; 

public: 
    float calculateMonthlyInterest() 
     {return savingBalance+=(savingBalance*annualInterestRate)/12;}//adding the interest to savingBalance 

    static void modifyInterestRate(float r) 
    {annualInterestRate=r;}//modify the annualInterestRate 

    SavingsAccount(float saving)//constructor with argument to set savingValue 
    {savingBalance=saving;} 

}; 


int main() 
{ 
SavingsAccount saver1(2000.00), saver2(3000.00);//instantiate 2 different SavingsAccount object 

SavingsAccount::modifyInterestRate(0.03);//set new interest to 3% 
//printing savers' new balance after 3% interest applied 
cout<<"THIS MONTH (3% INTEREST) :\n"; 
cout<<fixed<<setprecision(2)<<"Saver 1 balance : RM "<<saver1.calculateMonthlyInterest(); 
cout<<"\nSaver 2 balance : RM "<<saver2.calculateMonthlyInterest(); 

SavingsAccount::modifyInterestRate(0.04);//set new interest to 4% 
//printing savers' new balance after 4% interest applied 
cout<<"\n\nNEXT MONTH (4% INTEREST) :\n"; 
cout<<"Saver 1 balance : RM "<<saver1.calculateMonthlyInterest(); 
cout<<"\nSaver 2 balance : RM "<<saver2.calculateMonthlyInterest(); 

return 0; 

}

die vollständige Fehlermeldung: C: \ Benutzer \ NURULA ~ 1 \ AppData \ Local \ Temp \ ccOIgGs2.o Klasse Übung 3 Nr 1 ver 2.cpp :(rdata $ .refptr._ZN14SavingsAccount18annualInterestRateE [.refptr._ZN14SavingsAccount18annualInterestRateE] + 0x0.): undefined reference to `Savings :: annualInterestRate‘

und dieses Bild ist eine Momentaufnahme der Frage zu tun, ich versuche: the question

Die Frage hat nicht gefragt, einen Konstruktor zu machen, aber ich habe einen gemacht, vorausgesetzt, ich muss den Balance-Wert initialisieren, aber ich habe irgendwie das Gefühl, was das Problem und die Fehlermeldung verursacht. habe ich recht? .... oder falsch?

Vielen Dank im Voraus.

Antwort

2

Sie benötigen die annualInterestRate Variable irgendwo zu definieren, mit:

float SavingsAccount::annualInterestRate; 

Statische Variablen wie globale Variablen sind; Sie haben separate Deklarationen und Definitionen.