对“SavingsAccount::annualInterestRate”的未定义引用
undefined reference to `SavingsAccount::annualInterestRate'
我在编译我的 C++ 代码时偶然发现了这个错误。
这是我的代码:
#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;
}
完整的错误信息:
C:\Users\NURULA~1\AppData\Local\Temp\ccOIgGs2.o class 练习 3 第 1 ver 2.cpp:(.rdata$.refptr._ZN14SavingsAccount18annualInterestRateE[.refptr._ZN14SavingsAccount18annualInterestRateE]+0x0): undefined参考`SavingsAccount::annualInterestRate'
这张照片是我正在尝试做的问题的快照:
这个问题并没有要求制作一个构造函数,但我做了一个,假设我需要初始化余额值,但我有点感觉这就是导致问题和错误消息的原因。我对吗? ....还是错了?
先谢谢你。
您需要在某处定义 annualInterestRate
变量,其中:
float SavingsAccount::annualInterestRate;
静态变量就像全局变量;他们有单独的声明和定义。
我在编译我的 C++ 代码时偶然发现了这个错误。 这是我的代码:
#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;
}
完整的错误信息: C:\Users\NURULA~1\AppData\Local\Temp\ccOIgGs2.o class 练习 3 第 1 ver 2.cpp:(.rdata$.refptr._ZN14SavingsAccount18annualInterestRateE[.refptr._ZN14SavingsAccount18annualInterestRateE]+0x0): undefined参考`SavingsAccount::annualInterestRate'
这张照片是我正在尝试做的问题的快照:
这个问题并没有要求制作一个构造函数,但我做了一个,假设我需要初始化余额值,但我有点感觉这就是导致问题和错误消息的原因。我对吗? ....还是错了?
先谢谢你。
您需要在某处定义 annualInterestRate
变量,其中:
float SavingsAccount::annualInterestRate;
静态变量就像全局变量;他们有单独的声明和定义。