Newbie question : constructor return type ?

C

Chun Wah

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)
{}
 
A

Alf P. Steinbach

* Chun Wah said:
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.
 
C

Christian Jaeger

You're missing a semicolon:

class account{
// ...
};

account::account ...

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

HTH
 
A

Ahti Legonkov

Chun said:
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.
 
C

Chun Wah

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





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





Use different classes, possibly derived classes.





Use an enum.





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





The names here are unreadable.






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;






Needs semicolon here.





OK except for the names.

Got it, thanks alot
 
N

Nick Hounsome

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top