savings with statics c++

This is a discussion on savings with statics c++ within the C++ Programming forums, part of the General Programming Boards category; I'm learning how to use static members/functions and what the question provided I've came up with this so far. When ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    savings with statics c++

    I'm learning how to use static members/functions and what the question provided I've came up with this so far. When I run this code I get the follow message
    Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
    i changed around some settings trying to see if I could fix it when i changed my print to a double this error would go away but then i'd get some linker error. i went the the MS place to see if i could understand the error better but no like. Any help would be great

    Code:
    header file:
    class SavingsAccount 
    { 
    public: 
    
       SavingsAccount( double, double ); 
       void setAnnualInterestRate( double ); 
       void calculateMonthlyInterest(); 
       static void modifyInterestRate( double ); 
       void print(); 
    private: 
       double savingsBalance; 
       static double annualInterestRate; 
    };
    body:
    #include <iostream> 
    #include <iomanip> 
    using namespace std;  
    #include "savings.h" 
    
    double SavingsAccount::annualInterestRate = 0.0; //inital rate
     
    SavingsAccount::SavingsAccount( double bal, double annual ) 
    { 
       savingsBalance = ( bal >= 0.0 ? bal : 0.0 ); 
       setAnnualInterestRate( annual ); 
    } 
     
    void SavingsAccount::setAnnualInterestRate( double annual) 
    { 
       annualInterestRate = ( annual >= 0.0 && annual <= 1.0) ? annual : .03; 
    }
    
    void SavingsAccount::calculateMonthlyInterest() 
    { 
       savingsBalance += savingsBalance * ( annualInterestRate / 12); 
    } 
    void SavingsAccount::modifyInterestRate( double interest ) 
    { 
       annualInterestRate = (interest >= 0.0 && interest <= 1.0) ? interest : .04; 
    } 
    
    void SavingsAccount::print()
    { 
       cout << setprecision(2) << "$" << savingsBalance;
      
    } 
    int Main()
    {
    SavingsAccount server1(2000,.03);
    SavingsAccount server2(3000,.03);
    
    	
    cout << "Initial Balance For server1 Is: " << server1.print(); 
    cout << "\nInitial Balance For server2 Is: " << server2.print() <<  "\n\n"; 
    system("PAUSE");
    return 0;
    };

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    cout << "Initial Balance For server1 Is: " << server1.print(); 
    cout << "\nInitial Balance For server2 Is: " << server2.print() <<  "\n\n";
    For it to work the way you've got things now, you probably want:
    Code:
    cout << "Initial Balance For server1 Is: ";
    server1.print(); 
    cout << "\nInitial Balance For server2 Is: ";
    server2.print();
    cout << "\n\n";
    Otherwise you'll need to overload operator<< for your class for that to work as you were attempting. The basic problem is that your print function returns a void and you're attempting to print that void for which there is no operator<< defined.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    cool thanks, how does one overload cout tho if i wanted to do that?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    An example of the basic structure for overloading operator<< for a class:
    Code:
    class foo
    {
        int blah;
    public:
        std::ostream& print(std::ostream& os) const;
    };
    
    // Public "print" member function
    std::ostream& foo::print(std::ostream& os) const
    {
        return os << blah;
    }
    
    // Overload operator<< for objects of type foo, calls foo's public print member function
    std::ostream& operator<<(std::ostream& os, const foo& rhs)
    {
        return rhs.print(os);
    }
    
    int main()
    {
        foo bar;
    
        // Call overloaded operator<< for objects of type foo
        std::cout << bar << std::endl;
    
        return 0;
    }
    It's missing stuff like constructors and methods to get/set the blah data member but that's the general idea.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting local time without daylight savings
    By underthesun in forum C Programming
    Replies: 5
    Last Post: 06-30-2009, 09:33 AM
  2. statics in headers
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2009, 09:49 AM
  3. Daylight Savings Time
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-04-2004, 04:47 PM
  4. Seeking Critique about my prog!
    By Yevmaster in forum C++ Programming
    Replies: 3
    Last Post: 05-26-2003, 01:04 PM
  5. [POLL] Bye, Bye Daylight Savings Time!
    By Cruxus in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-29-2001, 01:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21