Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie question : constructor return type ?

Reply
Thread Tools

Newbie question : constructor return type ?

 
 
Chun Wah
Guest
Posts: n/a
 
      04-02-2004
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)
{}

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-02-2004
* Chun Wah <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Christian Jaeger
Guest
Posts: n/a
 
      04-02-2004
You're missing a semicolon:

class account{
// ...
};

account::account ...

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

HTH

 
Reply With Quote
 
Ahti Legonkov
Guest
Posts: n/a
 
      04-02-2004
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

 
Reply With Quote
 
Chun Wah
Guest
Posts: n/a
 
      04-02-2004
Alf P. Steinbach wrote:
> * Chun Wah <(E-Mail Removed)> 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

 
Reply With Quote
 
Nick Hounsome
Guest
Posts: n/a
 
      04-03-2004

"Chun Wah" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)-cable.com...
> Alf P. Steinbach wrote:
> > * Chun Wah <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
NewBie - java constructor error can't find constructor psmith@mcwy.com Java 6 02-20-2007 04:29 AM
Map of Parent Type and calling child type copy constructor (try two) Brandt Dusthimer C++ 2 07-12-2006 08:35 PM
Map of Parent Type and calling child type copy constructor Brandt Dusthimer C++ 4 07-12-2006 08:19 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments