代码错误E0265

qq_44011481 2019-05-15 09:51:08
我完全按书上来的啊,怎么会报错 第一个头文件 #ifndef POINT_H
#define POINT_H
class Point
{
private:
float x, y;
public:
Point();
void initpoint(float x = 0, float y = 0);
void move(float offx, float offy);
float getX()const;
float getY()const;
};
void Point::initpoint(float x = 0, float y = 0)
{
this->x = x;
this->y = y;
}
void Point::move(float offx, float offy)
{
x += offx;
y += offy;
}
float Point::getX()const
{
return x;
}
float Point::getY()const {
return y;
}
#endif // !POINT_H 第二个头文件 #pragma once
#ifndef REG_h
#define REG_H
#include "point.h"
class reg : public Point{
void initreg(float x, float y, float w, float h);
float getw()const;
float geth()const;
private:
float w, h;
};
void reg::initreg(float x, float y, float w, float h)
{
initpoint(x, y);
this->w = w;
this->h = h;
}
float reg::geth()const {
return h;
}
float reg::getw()const {
return w;
}
#endif // !1 main函数 #include <iostream>
#include <cmath>
#include "reg.h"
#include "point.h"
using namespace std;
int main()
{
reg rec;
rec.initreg(2, 2, 20, 10);
rec.move(3, 2);
cout << rec.getX() << ",";
cout << rec.getY() << ",";
cout << rec.geth() << ",";
cout << rec.getw() << endl;
return 0;
}
...全文
822 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Italink 2019-05-15
  • 打赏
  • 举报
回复
Error1:c++class默认修饰符是private,而你的reg类中的几个函数都没有修饰符,故默认为私有,所以报错:不可访问

class reg : public Point {
public:			//增加修饰符public
	void initreg(float x, float y, float w, float h);
	float getw()const;
	float geth()const;
private:
	float w, h;
};
Error2:函数参数默认值只可以定义一次

class Point
{
private:
	float x, y;
public:
	Point() {}
	void initpoint(float x = 0, float y = 0);
	void move(float offx, float offy);
	float getX()const;
	float getY()const;
};
void Point::initpoint(float x, float y )	//去除这里的默认值
{
	this->x = x;
	this->y = y;
}
Error3:未实现point的无参构造函数

class Point
{
private:
	float x, y;
public:
	Point() {}		//如果你不想构造函数做任何事情,可以这样写
	void initpoint(float x = 0, float y = 0);
	void move(float offx, float offy);
	float getX()const;
	float getY()const;
};

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧