gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:38:36 25 4
gpt4 key购买 nike

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

矩形.h

class Rectangle {
private:
int length;
int width;

public:
Rectangle();
void setR(int, int);
int getLength();
int getWidth();
int getArea();
void displayR();
};

矩形.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;
}

盒子.h

class Box : public Rectangle {
private:
int height;

public:
Box();
void setBox(int);
int getHeight();
int getVolume();
void displayB();

};

盒子.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;

最佳答案

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

应该是

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

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

关于c++ - 使用 '&' 创建指向成员错误的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32658170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com