Thread: Why doesnt this work

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    Why doesnt this work

    Hi, when i compile this code, it has loads of errors, but its out of a book im learning from.

    its...

    [tag] This is the header file it tells me to make [/tag]

    Code:
     
    // Savings - define a class that includes the ability
    //                 to make a deposit
    
    class Savings
    
    {
    public:
    
    // declare but dont define member function
    
    float Deposit(float ammount);
    unsigned int accountNumber;
    float Balance;
    };
    [tag] This is the main part - its only a small crappy thing but im learning [/tag]

    Code:
    // SavingsClassInline - invoke a member function thats
    //                                  both declared and defined within
    //                                  the class student
    
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    #include "savings.h"
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    
    savings s;
    s.accountNumber = 123456;
    s.balance = 0.0;
    
    // now add something to the account
    
    cout <<"Depositing 10 to the account " << s.accountNumber <<
    endl;
    s.Deposit(10);
    cout <<"Balance is " << s.balance << endl;
    
    // wait until the user is ready before terminating the program
    // so the user can view the results
    system("PAUSE");
    return 0;
    }
    [tag]the errors are[/tag]
    Code:
    12    c:\docume~1\reece\mydocu~1\saving~1.cpp 
    `savings' undeclared (first use this function)
    
    12 c:\docume~1\reece\mydocu~1\saving~1.cpp
     (Each undeclared identifier is reported only once
    
    12 c:\docume~1\reece\mydocu~1\saving~1.cpp
     for each function it appears in.)
    
    12 c:\docume~1\reece\mydocu~1\saving~1.cpp
     parse error before `;'
    
    13 c:\docume~1\reece\mydocu~1\saving~1.cpp
     `s' undeclared (first use this function)
    please help me out. cheers. Hugo.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >12 c:\docume~1\reece\mydocu~1\saving~1.cpp
    >`savings' undeclared (first use this function)

    >13 c:\docume~1\reece\mydocu~1\saving~1.cpp
    > `s' undeclared (first use this function)
    Tokens are case sensitive. You did not define a class called savings but one called Savings.
    Code:
    int main(int nNumberofArgs, char* pszArgs[])
    I'm not so sure this is right either. Try using
    Code:
    int main (int argc, char *argv[])
    It would do you good to define the Deposit() function somewhere with its class.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    class Savings
    {
    
    ...
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
        savings s;
    Pay attention to capitalization, case is important.
    "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

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    cheers the caps worked. but now it says

    Code:
    14 c:\docume~1\reece\mydocu~1\saving~1.cpp
     `class savings' has no member named `balance'
    
    20 c:\docume~1\reece\mydocu~1\saving~1.cpp
     `class savings' has no member named `balance'

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    lol now i fixed that. a popup with green bar says

    Code:
    C:\DOCUME~1\Reece\LOCALS~1\Temp\cc8ccaaa.o: In function `main':
    //c/docume~1/reece/mydocu~1/saving~1.cpp:19: undefined reference to `savings::Deposit(float)'
    EDIT:

    i defined s.Deposit and now it says

    Code:
    15 c:\docume~1\reece\mydocu~1\saving~1.cpp
     invalid use of member (did you forget the `&' ?)

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by citizen
    Code:
    int main(int nNumberofArgs, char* pszArgs[])
    I'm not so sure this is right either. Try using
    Code:
    int main (int argc, char *argv[])
    Hum... Changing the name of a variable doesn't do anything at all mister.

    As for your last error, we would need the code to help you, wouldn't we ?

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    read my code at the top. all i changed was the cap S to s and added s.Deposit below s.Balance .

    its annoying the hell out of me. simple code from a book and i cannot get the thing to work. geez. lol


    Hugo.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It would help to see your new code (including the member functions). As a guess, when you changed the class name from Savings to savings, you may not have changed the function definitions from Savings::Deposit to savings::Deposit for example. I would suggest changing the name of the class back to Savings and instead, change your s variable declaration in main to use Savings as the type instead of savings. But again, without code this is all just a guess.
    "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

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    Ok sorry this is my code

    header
    Code:
    // Savings - define a class that includes the ability
    //           to make a deposit
    
    class savings
    {
    public:
    
    // declare but dont define member function
    
    float Deposit(float ammount);
    unsigned int accountNumber;
    float Balance;
    };
    main code
    Code:
    // SavingsClassInline - invoke a member function thats
    //                      both declared and defined within
    //                      the class student
    
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    #include "savings.h"
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    savings s;
    s.accountNumber = 123456;
    s.Balance = 0.0;
    s.Deposit;
    
    // now add something to the account
    
    cout <<"Depositing 10 to the account " << s.accountNumber <<
    endl;
    s.Deposit(10);
    cout <<"Balance is " << s.Balance << endl;
    
    // wait until the user is ready before terminating the program
    // so the user can view the results
    
    system("PAUSE");
    return 0;
    }
    giving the error i put in the last reply.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by citizen
    It would do you good to define the Deposit() function somewhere with its class.
    I pointed this out. Deposit() is a member function that needs to be defined in a c++ file. So define a member function that takes a float as a parameter and adds it to the balance.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    s.Deposit;
    It isn't a member variable and it's an incorrect way to call a function... what is this supposed to be doing here?
    "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

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    oh sorry missed u saying that.

    ok i get ya, but this may say dumb. how do i define it lol i forgot.. whats the code for it?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> int main(int nNumberofArgs, char* pszArgs[])
    That is perfectly legal, although it is not the most common way to name the main parameters.

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Hugo716
    oh sorry missed u saying that.

    ok i get ya, but this may say dumb. how do i define it lol i forgot.. whats the code for it?
    Code:
    float savings::Deposit(float ammount)
    {
        Balance += ammount;
        return Balance;  // I'm guessing this is what you want to have returned
    }
    This needs to be in a source file somewhere, either in your main source or some other source file.
    "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. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM