Undeclared Identifier error #C2065

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teddarr
    New Member
    • Oct 2006
    • 143

    Undeclared Identifier error #C2065

    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?

    Code:
    #ifndef SAVINGSACCOUNT_H
    #include <iostream>
    
    using namespace std;
    
    class SavingsAccount
    {
    public:
    	SavingsAccount(int objn, double rate);
    	SavingsAccount(int objn, string fn, string ln, double rate);
    	SavingsAccount(int objn, string fn, string ln, double bal, double rate);
    	void setName(string, string);
    //	string getName() const;
    	string getFirstName();// const;
    	string getLastName();// const;
    	void setBalance(double);
    	double getBalance();// const;
    	static void setInterestRate(double);
    	static double getInterestRate();
    	int getNumber();// const;
    	double calculateNewBalance(double oldBalance);
    	~SavingsAccount();
    	
    private:
    	string firstName;
    	string lastName;
    	double savingBalance;
    	
    //	const int objectnumber;	
    	int objectnumber;
    
    	static double AIR;
    };
    
    #endif
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    #include "SavingsAccount.h"
    
    double SavingsAccount::AIR = 0.05;
    
    //constructor with no parameters***************
    SavingsAccount::SavingsAccount(int objn, double rate)
    {	
    	objectnumber = objn;	
    	firstName = "";
    	lastName = "";
    	savingBalance = 0.00;
    	setInterestRate(rate);
    }
    //constructor with name parameters******************
    SavingsAccount::SavingsAccount(int objn, string fn, string ln, double rate)
    {
    	objectnumber = objn;	
    	setName(fn, ln);
    	setBalance(0.00);
    	setInterestRate(rate);
    }
    //constructor with name parameters and balance parameter*****
    SavingsAccount::SavingsAccount(int objn, string fn, string ln, double bal, double rate)
    {
    	objectnumber= objn;	
    	setName(fn, ln);
    	setBalance(bal);
    	setInterestRate(rate);
    }
    //void setName()*********************************
    void SavingsAccount::setName(string fn, string ln)
    {	
    	firstName = fn;
    	lastName = ln;
    }
    //string getName()const**************************
    string SavingsAccount::getFirstName() //const
    {
    	return firstName;
    }
    
    string SavingsAccount::getLastName() //const
    {
    	return lastName;
    }
    //void setBalance()******************************
    void SavingsAccount::setBalance(double bal)
    {
    	savingBalance = bal;
    }
    //double getBalance()const***********************
    double SavingsAccount::getBalance() //const
    {
    	return savingBalance;
    }
    //static void setInterestRate()******************
    //static void SavingsAccount::setInterestRate(double rate)
    static void setInterestRate(double rate)
    {
    	AIR = rate;
    }
    //static double getInterestRate()****************
    //static double SavingsAccount::getInterestRate()
    static double getInterestRate()
    {
    	return AIR;
    }
    //getNumber()***********************************
    int SavingsAccount::getNumber() //const
    {
    	return objectnumber;
    }
    //calculateNewBalance()***************************
    double SavingsAccount::calculateNewBalance(double oldBalance)
    {
    	double newBalance = oldBalance + (oldBalance * (AIR/12));
    	
    	return newBalance;
    }
    //destructor, message: "gone out of scope"***********
    SavingsAccount::~SavingsAccount()
    {
    	cout<<"Object "<<objectnumber<<" has gone out of scope"<<endl;
    }
    Thanks!
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    is AIR a member of savings (or savingsAccount or whatever)

    Comment

    • teddarr
      New Member
      • Oct 2006
      • 143

      #3
      Yes, it is.

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        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.....An d many better brains will correct me here, I'm sure

        Comment

        • teddarr
          New Member
          • Oct 2006
          • 143

          #5
          I've tried it as both private and public with the same result.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            You have to refer to it as SavingsAccount: :Air, and I think it should be public.

            Comment

            • teddarr
              New Member
              • Oct 2006
              • 143

              #7
              I just tried all the variations of that I can think of.

              Still can't get it to work.

              Any more ideas?

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                Originally posted by teddarr
                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
                Code:
                void SavingsAccount::setInterestRate(double rate)
                {
                	AIR = rate;
                }
                
                double SavingsAccount::getInterestRate()
                {
                	return AIR;
                }

                Comment

                • teddarr
                  New Member
                  • Oct 2006
                  • 143

                  #9
                  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?

                  Comment

                  • horace1
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 1510

                    #10
                    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.
                    Code:
                    #include <iostream>
                    using namespace std;
                    
                    class SavingsAccount
                    {
                    public:
                    	static void setInterestRate(double);  // declare static member functions
                    	static double getInterestRate();
                    private:
                    	static double AIR;                    // declare static data member
                    	
                    };
                    
                    double SavingsAccount::AIR = 0.05;        // define static data member
                    
                    // define static member functions
                    void SavingsAccount::setInterestRate(double rate)
                    {
                    	AIR = rate;
                    }
                    double SavingsAccount::getInterestRate()
                    {
                    	return AIR;
                    }
                    
                    // call static member functions
                    int main()
                    {
                        cout << SavingsAccount::getInterestRate () << endl;
                        SavingsAccount::setInterestRate (1.5);
                        cout << SavingsAccount::getInterestRate () << endl;
                        system("pause");
                    }
                    when run this gives
                    0.05
                    1.5
                    Press any key to continue . .

                    Comment

                    • teddarr
                      New Member
                      • Oct 2006
                      • 143

                      #11
                      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.

                      Comment

                      Working...