Reputation: 13
I'm new to c++. I'm doing a tutorial question regarding inheritance. I got this error "'Box::getVolume': Non-standard syntax; use '&' to create a pointer to member". Since I'm relatively new, I don't understand what is needed to fix it. This is my code.
Rectangle.h
class Rectangle {
private:
int length;
int width;
public:
Rectangle();
void setR(int, int);
int getLength();
int getWidth();
int getArea();
void displayR();
};
Rectangle.cpp
Rectangle::Rectangle(){}
void Rectangle::setR(int l, int w) {
length = l;
width = w;
}
int Rectangle::getLength() {
return length;
}
int Rectangle::getWidth() {
return width;
}
int Rectangle::getArea(){
return length*width;
}
void Rectangle::displayR() {
cout<<"Length: "<<getLength()<<endl;
cout<<"Width: "<<getWidth()<<endl;
}
Box.h
class Box : public Rectangle {
private:
int height;
public:
Box();
void setBox(int);
int getHeight();
int getVolume();
void displayB();
};
Box.cpp
Box::Box(){ }
void Box::setBox(int h){
height = h;
}
int Box::getHeight(){
return height;
}
int Box::getVolume(){
return getArea()*height;
}
void Box::displayB(){
cout<<"Box height: "<<getHeight()<<endl;
cout<<"Box volume: "<<getVolume()<<endl;
Upvotes: 1
Views: 8641
Reputation: 56567
int getVolume(){ return getArea()*height;}
should be
int Box::getVolume(){ return getArea()*height;}
Although this shouldn't trigger a compiler error (you'll get a linker error though since Box::getVolume()
remains undefined).
Upvotes: 1