本文介绍了静态问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 亲爱的, 我对静态类成员有一点问题,但是找不到 虽然它很容易你能帮我吗? #include< iostream> 使用命名空间std; class SavingsAccount { public: SavingsAccount(双倍金额){ savingsBalance =金额; } void calculateMonthlyInterest(){ double mi; //每月利息 mi = savingsBalance * annualInterestRate / 12; savingsBalance + = mi; } static void modifyInterestRate(double newrate){ annualInterestRate = newrate; } double savingsGet()const { return savingsBalance; } static void setannualInterestRate(double rate){ annualInterestRate = rate; } 私人: 静态双年度积分率; 双倍储蓄平衡; }; int main() { SavingsAccount saver1(2000.0); SavingsAccount saver2(3000.0); SavingsAccount :: setannualInterestRate(3.0); saver1.calculateMonthlyInterest(); saver2.calculateMonthlyInterest(); cout<< saver1.savingsGet()<< endl; cout<< saver2.savingsGet()<< endl; SavingsAccount :: modifyInterestRate(4.0); saver1.calculateMonthlyInterest(); saver2。 calculateMonthlyInterest(); cout<< saver1.savingsGet()<< endl; cout<< saver2.savingsGet()<<结束; 返回0; } 解决方案 utab skrev:亲爱的, 我对静态类成员有一点问题但是找不到它虽然很容易。请你帮助我好吗? 问题是什么? #include< iostream> 使用命名空间std; class SavingsAccount {公开: SavingsAccount(双倍金额){ savingsBalance =金额; } void calculateMonthlyInterest(){双mi; //每月利息 mi = savingsBalance * annualInterestRate / 12; savingsBalance + = mi; } static void modifyInterestRate(double newrate){ annualInterestRate = newrate; } double savingsGet()const { return savingsBalance; } static void setannualInterestRate(double rate){ annualInterestRate = rate; 私有: static double annualInterestRate; double savingsBalance; }; double SavingsAccount :: annualInterestRate = 0.0; < snip> - TB @ SWEDEN 对不起,问题是 /tmp/ccBiUko3.o:在功能中 `ReavingsAccount :: calculateMonthlyInterest() '': d2.cc :(。nuu.linkonce.t._ZN14SavingsAccount24calcul ateMonthlyInterestEv [SavingsAccount :: calculateMonthlyI nterest()] + 0xd): 未定义引用`SavingsAccount :: annualInterestRate'' /tmp/ccBiUko3.o:函数中 `ReavingsAccount :: modifyInterestRate(double)'': d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modify InterestRateEd [SavingsAccount :: modifyInterestRate(double)] + 0x19): 未定义引用`SavingsAccount :: annualInterestRate'' d2.cc :(。gn.linkonce.t._ZN14SavingsAccount18modify InterestRateEd [SavingsAccount :: modifyInterestRate(double)] + 0x1f): 未定义引用`SavingsAccount :: annualInterestRate'' /tmp/ccBiUko3.o:函数中 `ReavingsAccount :: setannualInterestRate(double )'': d2.cc :(。nuu.linkonce.t._ZN14SavingsAccount21setann ualInterestRateEd [SavingsAccount :: setannualInterestRate(double)] + 0x19): undefined reference to `SavingsAccount :: annualInterestRate'' d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setann ualInterestRateE d [SavingsAccount :: setannualInterestRate(double)] + 0x1f): 未定义引用`SavingsAccount :: annualInterestRate'' collect2:ld返回1退出状态 :)) utab写道:对不起,问题是 /tmp/ccBiUko3.o:功能`SavingsAccount :: calculateMonthlyInterest()'': d2.cc :(。nuu.linkonce .t._ZN14SavingsAccount24calcul ateMonthlyInterestEv [SavingsAccount :: calculateMonthlyInterest()] + 0xd):未定义引用`SavingsAccount :: annualInterestRate''(另外5个引用相同变量的地方) 好吧,正如所有错误消息所说,问题很简单。 SavingsAccount :: annualInterestRate未定义;所以定义它。 HTH, Michiel Salters Dear all, I have a little problem about static class members and could not findthat though it is very easy. Could you please help me? #include <iostream> using namespace std; class SavingsAccount{public:SavingsAccount(double amount){savingsBalance=amount;}void calculateMonthlyInterest(){double mi; // monthly interestmi=savingsBalance*annualInterestRate/12;savingsBalance+=mi;}static void modifyInterestRate(double newrate){annualInterestRate = newrate;}double savingsGet() const{return savingsBalance;}static void setannualInterestRate(double rate){annualInterestRate=rate;}private:static double annualInterestRate;double savingsBalance;}; int main(){SavingsAccount saver1(2000.0);SavingsAccount saver2(3000.0); SavingsAccount::setannualInterestRate(3.0); saver1.calculateMonthlyInterest();saver2.calculateMonthlyInterest(); cout << saver1.savingsGet() << endl;cout << saver2.savingsGet() << endl; SavingsAccount::modifyInterestRate(4.0); saver1.calculateMonthlyInterest();saver2.calculateMonthlyInterest(); cout << saver1.savingsGet() << endl;cout << saver2.savingsGet() << endl; return 0; } 解决方案 utab skrev: Dear all, I have a little problem about static class members and could not find that though it is very easy. Could you please help me?And the problem is? #include <iostream> using namespace std; class SavingsAccount{ public: SavingsAccount(double amount){ savingsBalance=amount; } void calculateMonthlyInterest(){ double mi; // monthly interest mi=savingsBalance*annualInterestRate/12; savingsBalance+=mi; } static void modifyInterestRate(double newrate){ annualInterestRate = newrate; } double savingsGet() const{ return savingsBalance; } static void setannualInterestRate(double rate){ annualInterestRate=rate; } private: static double annualInterestRate; double savingsBalance; }; double SavingsAccount::annualInterestRate = 0.0; <snip> --TB @ SWEDENSorry for that the problem is /tmp/ccBiUko3.o: In function`SavingsAccount::calculateMonthlyInterest()'':d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount24calcul ateMonthlyInterestEv[SavingsAccount::calculateMonthlyInterest()]+0xd):undefined reference to `SavingsAccount::annualInterestRate''/tmp/ccBiUko3.o: In function`SavingsAccount::modifyInterestRate(double)'':d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modify InterestRateEd[SavingsAccount::modifyInterestRate(double)]+0x19):undefined reference to `SavingsAccount::annualInterestRate''d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modify InterestRateEd[SavingsAccount::modifyInterestRate(double)]+0x1f):undefined reference to `SavingsAccount::annualInterestRate''/tmp/ccBiUko3.o: In function`SavingsAccount::setannualInterestRate(double)'':d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setann ualInterestRateEd[SavingsAccount::setannualInterestRate(double)]+0x19):undefined reference to `SavingsAccount::annualInterestRate''d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setann ualInterestRateEd[SavingsAccount::setannualInterestRate(double)]+0x1f):undefined reference to `SavingsAccount::annualInterestRate''collect2: ld returned 1 exit status :)) utab wrote: Sorry for that the problem is /tmp/ccBiUko3.o: In function `SavingsAccount::calculateMonthlyInterest()'': d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount24calcul ateMonthlyInterestEv[SavingsAccount::calculateMonthlyInterest()]+0xd): undefined reference to `SavingsAccount::annualInterestRate'' (5 more places in which the same variable is referenced) Well, as all the error messages say, the problem is simple.SavingsAccount::annualInterestRate is undefined; so define it. HTH,Michiel Salters 这篇关于静态问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 05:04