2016-04-07 1 views
0

Je suis tombé sur cette erreur lors de la compilation de mon code C++. voici mon code:référence non définie à `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; 

}

le message d'erreur complet: C: \ Users \ NURULA ~ 1 \ AppData \ Local \ Temp \ classe ccOIgGs2.o exercice 3 no 1 ver 2.cpp :(RDATA $ .refptr._ZN14SavingsAccount18annualInterestRateE [.refptr._ZN14SavingsAccount18annualInterestRateE] + 0x0):. undefined reference to `SavingsAccount :: annualInterestRate »

et cette image est un instantané de question que je suis en train de faire: the question

la question n'a pas demandé de faire un constructeur mais j'ai fait un, en supposant que je dois initialiser la valeur d'équilibre, mais j'ai un peu le sentiment que c'est ce qui cause le problème et le message d'erreur. ai-je raison? .... ou faux?

merci à l'avance.

Répondre

2

Vous devez définir la part variable de annualInterestRate, avec:

float SavingsAccount::annualInterestRate; 

Les variables statiques sont comme des variables globales; ils ont des déclarations et des définitions séparées.