c++ primer static成员解决方案
c++ primer static成员
照着书,抄了一段代码,但是编译出现LNK2001。。
#include <string>
using namespace std;
class Account
{
public:
Account();
void applyini(){amount += amount * interestRate;}
static double rate(){return interestRate;}
static void rate(double newRate);//sets a new rate
private:
string owner;
double amount;
static double interestRate;
static double initRate();
};
main函数中
Account ac1;
Account *ac2 = &ac1;
double rate;
rate = ac1.rate();
rate = ac2->rate();
rate = Account::rate();
错误 1 error LNK2001: 无法解析的外部符号 "private: static double Account::interestRate" (?interestRate@Account@@0NA) D:\VS workspace\PracticeTest\Chapter12Class\Chapter12Class\main.obj
------解决思路----------------------
书上应该有告诉你静态成员需要在类外声明吧
------解决思路----------------------
double Account::interestRate = 10; //静态成员变量需要初始化
------解决思路----------------------
只是声明了,没有定义。
------解决思路----------------------
淡定,别说自己笨,学习什么都是慢慢来的,一下就什么都会什么都能理解的那不是普通人。
照着书,抄了一段代码,但是编译出现LNK2001。。
#include <string>
using namespace std;
class Account
{
public:
Account();
void applyini(){amount += amount * interestRate;}
static double rate(){return interestRate;}
static void rate(double newRate);//sets a new rate
private:
string owner;
double amount;
static double interestRate;
static double initRate();
};
main函数中
Account ac1;
Account *ac2 = &ac1;
double rate;
rate = ac1.rate();
rate = ac2->rate();
rate = Account::rate();
错误 1 error LNK2001: 无法解析的外部符号 "private: static double Account::interestRate" (?interestRate@Account@@0NA) D:\VS workspace\PracticeTest\Chapter12Class\Chapter12Class\main.obj
------解决思路----------------------
书上应该有告诉你静态成员需要在类外声明吧
------解决思路----------------------
double Account::interestRate = 10; //静态成员变量需要初始化
------解决思路----------------------
只是声明了,没有定义。
------解决思路----------------------
淡定,别说自己笨,学习什么都是慢慢来的,一下就什么都会什么都能理解的那不是普通人。