派生类错误
Derived Classes Error
我使用派生类完成了一个小程序,它编译正确,但输出错误。
该程序使用有序对(x,y)作为圆心。然后,它使用给定的圆心和半径来确定圆的面积和周长。
使用pointType类输出有序对可以很好地工作,但当我测试circleType类时,我希望得到(0,0)作为默认值。相反,我得到了(2293512293700)
任何帮助都将不胜感激!
这是点类代码:
#ifndef POINTTYPE_H_INCLUDED
#define POINTTYPE_H_INCLUDED
#include <iostream>
class pointType{
public:
pointType (int=0, int=0);
int getX() const;
int getY() const;
void setX(int);
void setY(int);
void setValues(int, int);
friend pointType operator + (pointType, pointType);
friend pointType operator - (pointType, pointType);
friend std::ostream& operator << (std::ostream&, pointType);
private:
int x;
int y;
};
#endif // POINTTYPE_H_INCLUDED
以下是点类实现:
#include "pointType.h"
pointType::pointType (int X, int Y) : x(X), y(Y) {}
int pointType::getX () const {
return x;
}
int pointType::getY () const {
return y;
}
void pointType::setX (int new_x) {
x = new_x;
}
void pointType::setY (int new_y) {
y = new_y;
}
void pointType::setValues (int new_x, int new_y) {
x = new_x;
y = new_y;
}
pointType operator + (pointType lh, pointType rh){
pointType answer;
answer.x = lh.x + rh.x;
answer.y = lh.y + rh.y;
return answer;
}
pointType operator - (pointType lh, pointType rh){
pointType answer;
answer.x = lh.x - rh.x;
answer.y = lh.y - rh.y;
return answer;
}
std::ostream& operator << (std::ostream& out, pointType c){
out << "(" << c.x << ", " << c.y << ")";
return out;
}
这是圆圈类:
#ifndef CIRCLETYPE_H_INCLUDED
#define CIRCLETYPE_H_INCLUDED
#include "pointType.h"
#include <iostream>
class circleType: protected pointType {
public:
circleType (float=0);
circleType (int=0, int=0);
void setRadius (float);
float calculateArea (float);
float calculateCircumference (float);
friend std::ostream& operator << (std::ostream&, circleType);
private:
float radius;
int center_x;
int center_y;
};
#endif // CIRCLETYPE_H_INCLUDED
这是circle类的实现:
#include "pointType.h"
#include "circleType.h"
#include <math.h>
const float PI = 3.14;
circleType::circleType(float R): radius(R) {}
circleType::circleType(int center_X, int center_Y):
pointType(center_x, center_y) {}
void circleType::setRadius(float new_radius) {
radius = new_radius;
}
float circleType::calculateArea(float radius) {
float area;
area = PI * pow(radius, 2);
return area;
}
float circleType::calculateCircumference(float radius) {
float circumference;
circumference = PI * (radius * 2);
return circumference;
}
std::ostream& operator << (std::ostream& odata, circleType f) {
odata << "(" << f.center_x << ", " << f.center_y << ")";
return odata;
}
这是测试代码:
#include "pointType.h"
#include "circleType.h"
#include <iostream>
#include <math.h>
using namespace std;
int main() {
pointType c, d(8, 9);
circleType f(4, 5);
cout << c << endl;
cout << d << endl;
c.setValues(12, 3);
cout << c << endl;
cout << c + d << endl;
cout << c - d << endl;
cout << f << endl;
return 0;
}
据我所见,circleType int成员center_x和center_y从未设置。构造函数CircleType::CircleType(int=0,int=0)调用基构造函数,而基构造函数又设置了pointType.x和pointType.y,这两个成员都是基类的私有成员(因此CircleType不可用)。
我的建议(或者我认为你想要的):让int x和int y受到保护,而不是私有的,删除center_x和center_y,然后打印odata<lt;("<<f.x<","<<f.y<)";
这是一个拼写错误。
circleType::circleType(int center_X, int center_Y):
pointType(center_x, center_y) {}
请注意center_X(大写)和center_X之间的区别。您将未初始化的成员传递给父构造函数,该构造函数可以在堆栈中具有任何延迟值。
因此,经常用m_
(例如:m_centerX
)指导您的成员是一种很好的做法。(在这种特殊情况下,甚至没有必要在子类中再次使用它们,冗余是不好的!)
编辑:
很抱歉造成混淆,但下次请说得更准确一点,本可以为我们节省很多时间。我假设你的问题发生在用圆圈添加点时,在我自己运行代码后,我注意到它发生在输出f时。原因与上面解释的相同:你没有初始化你的成员!它们只是保存当前位于对象构建的内存位置的任何值。
在circleType类中,center_x和center_y没有初始化。参数被直接传递给pointType构造函数并设置私有成员。您可以通过更改运算符<lt;接受circleType参数的函数:
std::ostream& operator << (std::ostream& odata, circleType f) {
odata << "(" << f.center_x << ", " << f.center_y << ")" << std::endl;
odata << "(" << f.getX() << ", " << f.getY() << ")"; //access superclass
return odata;
}
对于受保护的继承,您也不能使用为基类定义的+和-重载,因为它不会编译。
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- C++如果采用类类型的函数被传递派生类型,有没有办法给出错误?
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 返回派生类型时出现协变类型错误
- 错误:基类在从基类父派生类 Son 时未定义
- 通过基类引用派生类后打印的错误值
- 尝试删除指向派生对象的基指针时断言错误
- C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板专用化
- 由于从 std::map 派生的类中的 std::map 迭代器导致的内存错误
- 如何修复派生类给出错误'declaration has no storage class or type specifier'?
- 简单派生类上的Linux GCC链接错误
- 通过参考将派生的类传递给运算符会产生错误
- 使用从WINRT网格类派生的抽象类时,Casttobase未解决的外部符号错误
- 尝试通过基类指针访问派生的类成员函数将导致什么样的错误
- 在派生类CTOR中接收基类副本是错误的吗?
- 在向量中使用派生的可移动但不可压缩的会导致编译错误
- 为什么当我有两个函数时编译器没有显示错误,一个将采用基类,一个将派生类作为参数
- 错误C2509:成员函数未在派生类中声明
- std::unique_ptr 编译器错误:派生类的成员无法访问基类的私有成员
- C++模板参数失败,不是从错误派生的