By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

Undeclared Identifier error #C2065

100+
P: 143
I am trying to construct a class with several functions and identifiers. The identifier in question is static double AIR which will hold the value of the annual interest rate in the class.

I have been getting an error message that I can't seem to get rid of:

error C2065: 'AIR' undeclared identifier

I have never worked with static identifiers before and I think I'm leaving something out somewhere but I don't know what.

Here's the header and .cpp files:

Any Ideas?

Expand|Select|Wrap|Line Numbers
  1. #ifndef SAVINGSACCOUNT_H
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class SavingsAccount
  7. {
  8. public:
  9.     SavingsAccount(int objn, double rate);
  10.     SavingsAccount(int objn, string fn, string ln, double rate);
  11.     SavingsAccount(int objn, string fn, string ln, double bal, double rate);
  12.     void setName(string, string);
  13. //    string getName() const;
  14.     string getFirstName();// const;
  15.     string getLastName();// const;
  16.     void setBalance(double);
  17.     double getBalance();// const;
  18.     static void setInterestRate(double);
  19.     static double getInterestRate();
  20.     int getNumber();// const;
  21.     double calculateNewBalance(double oldBalance);
  22.     ~SavingsAccount();
  23.  
  24. private:
  25.     string firstName;
  26.     string lastName;
  27.     double savingBalance;
  28.  
  29. //    const int objectnumber;    
  30.     int objectnumber;
  31.  
  32.     static double AIR;
  33. };
  34.  
  35. #endif
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. #include "SavingsAccount.h"
  7.  
  8. double SavingsAccount::AIR = 0.05;
  9.  
  10. //constructor with no parameters***************
  11. SavingsAccount::SavingsAccount(int objn, double rate)
  12. {    
  13.     objectnumber = objn;    
  14.     firstName = "";
  15.     lastName = "";
  16.     savingBalance = 0.00;
  17.     setInterestRate(rate);
  18. }
  19. //constructor with name parameters******************
  20. SavingsAccount::SavingsAccount(int objn, string fn, string ln, double rate)
  21. {
  22.     objectnumber = objn;    
  23.     setName(fn, ln);
  24.     setBalance(0.00);
  25.     setInterestRate(rate);
  26. }
  27. //constructor with name parameters and balance parameter*****
  28. SavingsAccount::SavingsAccount(int objn, string fn, string ln, double bal, double rate)
  29. {
  30.     objectnumber= objn;    
  31.     setName(fn, ln);
  32.     setBalance(bal);
  33.     setInterestRate(rate);
  34. }
  35. //void setName()*********************************
  36. void SavingsAccount::setName(string fn, string ln)
  37. {    
  38.     firstName = fn;
  39.     lastName = ln;
  40. }
  41. //string getName()const**************************
  42. string SavingsAccount::getFirstName() //const
  43. {
  44.     return firstName;
  45. }
  46.  
  47. string SavingsAccount::getLastName() //const
  48. {
  49.     return lastName;
  50. }
  51. //void setBalance()******************************
  52. void SavingsAccount::setBalance(double bal)
  53. {
  54.     savingBalance = bal;
  55. }
  56. //double getBalance()const***********************
  57. double SavingsAccount::getBalance() //const
  58. {
  59.     return savingBalance;
  60. }
  61. //static void setInterestRate()******************
  62. //static void SavingsAccount::setInterestRate(double rate)
  63. static void setInterestRate(double rate)
  64. {
  65.     AIR = rate;
  66. }
  67. //static double getInterestRate()****************
  68. //static double SavingsAccount::getInterestRate()
  69. static double getInterestRate()
  70. {
  71.     return AIR;
  72. }
  73. //getNumber()***********************************
  74. int SavingsAccount::getNumber() //const
  75. {
  76.     return objectnumber;
  77. }
  78. //calculateNewBalance()***************************
  79. double SavingsAccount::calculateNewBalance(double oldBalance)
  80. {
  81.     double newBalance = oldBalance + (oldBalance * (AIR/12));
  82.  
  83.     return newBalance;
  84. }
  85. //destructor, message: "gone out of scope"***********
  86. SavingsAccount::~SavingsAccount()
  87. {
  88.     cout<<"Object "<<objectnumber<<" has gone out of scope"<<endl;
Thanks!
Feb 23 '07 #1
Share this Question
Share on Google+
10 Replies


DeMan
100+
P: 1,806
is AIR a member of savings (or savingsAccount or whatever)
Feb 23 '07 #2

100+
P: 143
Yes, it is.
Feb 23 '07 #3

DeMan
100+
P: 1,806
If it's private, it can't be accessed publicly?
I'm not sur eabout this, but I don't think it's visible the way that it has been declared.....And many better brains will correct me here, I'm sure
Feb 23 '07 #4

100+
P: 143
I've tried it as both private and public with the same result.
Feb 23 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
You have to refer to it as SavingsAccount::Air, and I think it should be public.
Feb 23 '07 #6

100+
P: 143
I just tried all the variations of that I can think of.

Still can't get it to work.

Any more ideas?
Feb 23 '07 #7

Expert 100+
P: 1,510
I just tried all the variations of that I can think of.

Still can't get it to work.

Any more ideas?
try defining setInterestRate() and getInterestRate() so
Expand|Select|Wrap|Line Numbers
  1. void SavingsAccount::setInterestRate(double rate)
  2. {
  3.     AIR = rate;
  4. }
  5.  
  6. double SavingsAccount::getInterestRate()
  7. {
  8.     return AIR;
  9. }
  10.  
Feb 24 '07 #8

100+
P: 143
horace1, I tried but it doesn't work like I need it to.

AIR is supposed to be a static data member. It's the static part that is getting me mixed up. How do I declare AIR as a static member? setInterestRate() and getInterestRate() are supposed to be static functions also.

I can't seem to get this to work right. What am I missing?
Feb 25 '07 #9

Expert 100+
P: 1,510
you declare a static data member (it name and type) within the class declaration and you define it (allocate it storage and initial value) outside the class declaration (without the keyword static). e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class SavingsAccount
  5. {
  6. public:
  7.     static void setInterestRate(double);  // declare static member functions
  8.     static double getInterestRate();
  9. private:
  10.     static double AIR;                    // declare static data member
  11.  
  12. };
  13.  
  14. double SavingsAccount::AIR = 0.05;        // define static data member
  15.  
  16. // define static member functions
  17. void SavingsAccount::setInterestRate(double rate)
  18. {
  19.     AIR = rate;
  20. }
  21. double SavingsAccount::getInterestRate()
  22. {
  23.     return AIR;
  24. }
  25.  
  26. // call static member functions
  27. int main()
  28. {
  29.     cout << SavingsAccount::getInterestRate () << endl;
  30.     SavingsAccount::setInterestRate (1.5);
  31.     cout << SavingsAccount::getInterestRate () << endl;
  32.     system("pause");
  33. }
  34.  
  35.  
when run this gives
0.05
1.5
Press any key to continue . .
Feb 25 '07 #10

100+
P: 143
Thank you so much. I think that is the simple, get right to it answer I've been needing.

I'll let you know how it turns out when I get a chance.

Thanks again.
Feb 25 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.