Reputation: 268
I haven't used c++ for a long time and I never really did grasp classes too well.
I decided to relearn classes by making a small geometry app.
Here is square.h:
class Square{
public:
float width;
float height;
float area;
float perimeter;
void Square(int,int);
void Square();
void ~Square();
};
Here is square.cpp:
#include "square.h"
Square::Square (int w, int h){
width = w;
height = h;
area = width * height;
perimeter = (width*2)+(height*2);
}
Square::Square (){
}
Square::~Square (){
}
When I run/build the program it says error: return type specification for constructor invalid
I guess this is saying the constructors and destructors should be something other than void
, but I think I'm wrong.
Upvotes: 0
Views: 166
Reputation: 576
Constructor
and destructor
have no return type. They are a special kind of function of class having the same name of the class
.
class Square{
public:
float width;
float height;
float area;
float perimeter;
Square(int,int);
Square();
~Square();
};
Upvotes: 0
Reputation: 1246
In constructors and destructors there should be no return type at all,
class Square
{
public:
float width;
float height;
float area;
float perimeter;
Square(int,int);
Square();
~Square();
};
Upvotes: 0
Reputation: 899
get rid of void
in constructors and destructor.
Square(int,int);
Square();
~Square();
And a suggestion since you are learning. If you are not thinking of exposing class variables to subclasses, make them private.
Upvotes: 0
Reputation: 59363
I guess this is saying the constructors and destructors should be something other than
void
Yes, it should be:
Square(int,int);
Square();
~Square();
I thought
void
means the function doesn't return anything?
Yes, but these are not functions. They're constructors and destructors, which don't need a specified return type.
Upvotes: 3