使用'&'创建指向成员错误的指针

时间:2015-09-18 17:51:18

标签: c++ inheritance

我是c ++的新手。我正在做关于继承的教程问题。我收到此错误“'Box :: getVolume':非标准语法;使用'&'创建指向成员的指针“。由于我比较新,我不明白修复它需要什么。这是我的代码。

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;

1 个答案:

答案 0 :(得分:1)

int getVolume(){ return getArea()*height;}

应该是

int Box::getVolume(){ return getArea()*height;}

虽然这不应该触发编译器错误(但是由于Box::getVolume()仍未定义,因此您会收到链接器错误。)