Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,383 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,185 people online right now. Registration is fast and FREE... Join Now!




newbie in c++

 
Reply to this topicStart new topic

newbie in c++

jorijo
post 22 Aug, 2008 - 02:58 AM
Post #1


New D.I.C Head

*
Joined: 16 Aug, 2006
Posts: 9


My Contributions


Hi! I'm new to c++ and I'm following the examples of a book,but get stucked in this example using constructors... the compiler give me the following errors:

'saving_account' : undeclared identifier
' missing ';' before identifier 'acc1'
'acc1': identifier not found

CODE
//main
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void main()
{
    int id;
    double bal;
    double rt;
    
    cout << "\n Enter an id account: ";
    cin >> id;

    cout << "\n Enter balance: ";
    cin >> bal;

    cout << "\n Enter interest rate: ";
    cin >> rt;

    saving_account acc1(id, bal, rt);
}

//class declaration

class saving_account
{
private:
    string id_no;
    double balance;
    double rate;

public:
    saving_account(string, double, double);
    double calc_interest();
};

saving_account::saving_account(string id, double bal, double rt)
{
    id_no = id;
    balance = bal;
    rate = rt;
}

double saving_account::calc_interest()
{
    return balance * rate;
}
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 22 Aug, 2008 - 03:31 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Try putting your class before main.
User is offlineProfile CardPM

Go to the top of the page

jorijo
post 22 Aug, 2008 - 03:39 AM
Post #3


New D.I.C Head

*
Joined: 16 Aug, 2006
Posts: 9


My Contributions


I have the code in 2 files... the class is in the header file and the main is in the .ccp file. do you think that is the cause of the errors?!
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 22 Aug, 2008 - 03:55 AM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


In which case, I don't see you using anything like:
#include "myHeaderTitleHere.h"


You said you were having troubles with constructors? I just read over it, it looks good to me icon_up.gif

One thing that you might want to do is use the this pointer. this basically refers to "this object" like so:
cpp
saving_account::saving_account(string id, double bal, double rt)
{
this->id_no = id;
this->balance = bal;
this->rate = rt;
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:36AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month