Newbie question : constructor return type ?

Discussion in 'C++' started by Chun Wah, Apr 2, 2004.

  1. Chun Wah

    Chun Wah Guest

    I am new to C++ and trying to write a accounting application class. This
    "account" class is derived from base "books" which has 4 data. I got a
    compile error as follow and not sure what goes wrong.

    75: error: ISO C++ forbids defining types
    within return type
    75: error: return type specification for
    constructor invalid

    line 75 is in the constructor.

    Another question is: is there a return type of a constructor ? isnt it
    the object itself ?


    class account
    {
    private:

    std::string acname;
    int level;
    bool generaldetail; //0-general, 1-detail
    int type; //1-asset, 2-liability, 3-capital, 4-income, 5-expense
    float balance;
    bool recorded;

    public:

    account (const std::string& nn, int lvl, bool gd, int t,
    float bal, bool rec);
    std::string getname(void);
    int getlevel(void);
    bool getgeneraldetail(void);
    int gettype(void);
    float getbalance(void);
    bool getrecorded(void);
    }

    // constructor

    account::account (const std::string& nn, int lvl, bool gd, int t,
    float bal, bool rec): // line 75
    acname(nn),
    level(lvl),
    generaldetail(gd),
    type(t),
    balance(bal),
    recorded(rec)
    {}
    Chun Wah, Apr 2, 2004
    #1
    1. Advertising

  2. * Chun Wah <> schriebt:
    > I am new to C++ and trying to write a accounting application class. This
    > "account" class is derived from base "books" which has 4 data. I got a
    > compile error as follow and not sure what goes wrong.
    >
    > 75: error: ISO C++ forbids defining types
    > within return type
    > 75: error: return type specification for
    > constructor invalid
    >
    > line 75 is in the constructor.


    You need a semicolon after the closing brace of the class declaration.


    >
    > Another question is: is there a return type of a constructor ? isnt it
    > the object itself ?


    A constructor does not have a return type, and yes it is _in a sense_ the
    object itself, which the constructor constructs.


    > class account
    > {
    > private:
    >
    > std::string acname;
    > int level;
    > bool generaldetail; //0-general, 1-detail


    Use different classes, possibly derived classes.


    > int type; //1-asset, 2-liability, 3-capital, 4-income, 5-expense


    Use an enum.


    > float balance;


    float is not a good data type for monetary values because it
    has few significant digits.


    > bool recorded;
    >
    > public:
    >
    > account (const std::string& nn, int lvl, bool gd, int t,
    > float bal, bool rec);


    The names here are unreadable.



    > std::string getname(void);


    void as argument type is C'ism.

    the name of a function that returns something should ideally reflect
    what it returns, not what it does in order to return that; hence, "get"
    is superfluous and possibly misdirecting.

    A function that does not modify the object should be 'const'

    std::string name() const;



    > int getlevel(void);
    > bool getgeneraldetail(void);
    > int gettype(void);
    > float getbalance(void);
    > bool getrecorded(void);
    > }


    Needs semicolon here.


    > // constructor
    >
    > account::account (const std::string& nn, int lvl, bool gd, int t,
    > float bal, bool rec): // line 75
    > acname(nn),
    > level(lvl),
    > generaldetail(gd),
    > type(t),
    > balance(bal),
    > recorded(rec)
    > {}


    OK except for the names.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Apr 2, 2004
    #2
    1. Advertising

  3. You're missing a semicolon:

    class account{
    // ...
    };

    account::account ...

    Without the semicolon, your compiler takes class account as return type
    for acount::account

    HTH
    Christian Jaeger, Apr 2, 2004
    #3
  4. Chun Wah wrote:

    > I am new to C++ and trying to write a accounting application class. This
    > "account" class is derived from base "books" which has 4 data. I got a
    > compile error as follow and not sure what goes wrong.
    >
    > 75: error: ISO C++ forbids defining types
    > within return type
    > 75: error: return type specification for
    > constructor invalid
    >
    > line 75 is in the constructor.
    >
    > Another question is: is there a return type of a constructor ? isnt it
    > the object itself ?
    >
    >
    > class account
    > {
    > private:
    >
    > std::string acname;
    > int level;
    > bool generaldetail; //0-general, 1-detail
    > int type; //1-asset, 2-liability, 3-capital, 4-income, 5-expense


    for type use an enum rather than magic numbers:

    enum types {
    asset = 1,
    liability = 2,
    capital = 3,
    income = 4,
    expense = 5,
    };
    types type;

    > float balance;
    > bool recorded;
    >
    > public:
    >
    > account (const std::string& nn, int lvl, bool gd, int t,
    > float bal, bool rec);
    > std::string getname(void);
    > int getlevel(void);
    > bool getgeneraldetail(void);
    > int gettype(void);
    > float getbalance(void);
    > bool getrecorded(void);
    > }

    ---^
    there is a semicolon (;) missing here. Without it compiler thinks that
    you want to define a new type that is return type for function
    account::account.

    > // constructor
    >
    > account::account (const std::string& nn, int lvl, bool gd, int t,
    > float bal, bool rec): // line 75
    > acname(nn),
    > level(lvl),
    > generaldetail(gd),
    > type(t),
    > balance(bal),
    > recorded(rec)
    > {}
    >



    --
    Ahti Legonkov
    Ahti Legonkov, Apr 2, 2004
    #4
  5. Chun Wah

    Chun Wah Guest

    Alf P. Steinbach wrote:
    > * Chun Wah <> schriebt:
    >
    >>I am new to C++ and trying to write a accounting application class. This
    >>"account" class is derived from base "books" which has 4 data. I got a
    >>compile error as follow and not sure what goes wrong.
    >>
    >>75: error: ISO C++ forbids defining types
    >> within return type
    >>75: error: return type specification for
    >> constructor invalid
    >>
    >>line 75 is in the constructor.

    >
    >
    > You need a semicolon after the closing brace of the class declaration.
    >
    >
    >
    >>Another question is: is there a return type of a constructor ? isnt it
    >>the object itself ?

    >
    >
    > A constructor does not have a return type, and yes it is _in a sense_ the
    > object itself, which the constructor constructs.
    >
    >
    >
    >>class account
    >>{
    >> private:
    >>
    >> std::string acname;
    >> int level;
    >> bool generaldetail; //0-general, 1-detail

    >
    >
    > Use different classes, possibly derived classes.
    >
    >
    >
    >> int type; //1-asset, 2-liability, 3-capital, 4-income, 5-expense

    >
    >
    > Use an enum.
    >
    >
    >
    >> float balance;

    >
    >
    > float is not a good data type for monetary values because it
    > has few significant digits.
    >
    >
    >
    >> bool recorded;
    >>
    >> public:
    >>
    >> account (const std::string& nn, int lvl, bool gd, int t,
    >> float bal, bool rec);

    >
    >
    > The names here are unreadable.
    >
    >
    >
    >
    >> std::string getname(void);

    >
    >
    > void as argument type is C'ism.
    >
    > the name of a function that returns something should ideally reflect
    > what it returns, not what it does in order to return that; hence, "get"
    > is superfluous and possibly misdirecting.
    >
    > A function that does not modify the object should be 'const'
    >
    > std::string name() const;
    >
    >
    >
    >
    >> int getlevel(void);
    >> bool getgeneraldetail(void);
    >> int gettype(void);
    >> float getbalance(void);
    >> bool getrecorded(void);
    >>}

    >
    >
    > Needs semicolon here.
    >
    >
    >
    >>// constructor
    >>
    >>account::account (const std::string& nn, int lvl, bool gd, int t,
    >> float bal, bool rec): // line 75
    >> acname(nn),
    >> level(lvl),
    >> generaldetail(gd),
    >> type(t),
    >> balance(bal),
    >> recorded(rec)
    >> {}

    >
    >
    > OK except for the names.
    >


    Got it, thanks alot
    Chun Wah, Apr 2, 2004
    #5
  6. "Chun Wah" <> wrote in message
    news:-cable.com...
    > Alf P. Steinbach wrote:
    > > * Chun Wah <> schriebt:
    > >
    > >>I am new to C++ and trying to write a accounting application class. This
    > >>"account" class is derived from base "books" which has 4 data. I got a
    > >>compile error as follow and not sure what goes wrong.


    You got a good answer already.

    I'll give you a good debugging tip for future use:

    If you get a cryptic error message like this try putting a simple statement
    just before it - if the error moves to the simple statement then you know it
    was some sort of termination problem with the stuff before. eg. add the
    following before the line giving the error.

    int x; // too simple to be wrong
    Nick Hounsome, Apr 3, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    1,108
  2. Greenhorn
    Replies:
    15
    Views:
    811
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    6
    Views:
    501
  4. Brandt Dusthimer
    Replies:
    4
    Views:
    307
    Jim Langston
    Jul 12, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,218
Loading...

Share This Page