为什么我得到这个错误的封装在c++
Why am I getting this error on encapsulation in C++?
这是我的Line2D.h
class Line2D
{
private:
Point2D pt1;
Point2D pt2;
protected:
double length;
// Set function
void setLength();
public:
// Constructor
Line2D();
Line2D(Point2D pt1, Point2D pt2);
// Get functions
Point2D getPt1();
Point2D getPt2();
double getScalarValue();
// Set functions
void setPt1(Point2D pt1);
void setPt2(Point2D pt2);
// Other functions
void printLine2D();
bool operator==(const Line2D& otherL2D) const;
};
这是我的Point2D.h,
class Point2D
{
// friend bool operator==(const Point2D& otherP2D) const;
protected:
// private:
// public:
int x;
int y;
double distFrOrigin;
// This function computes the distance of
// the point to the origin (0,0) and initializes
// disFrOrigin with the distance value.
void setDistFrOrigin();
public:
// Constructor
Point2D();
Point2D(int x, int y);
// Get functions
int getX();
int getY();
// Set functions
void setX(int);
void setY(int);
// Accessor method(returns the value of distFrOrigin
double getScalarValue();
void printPoint2D();
bool operator==(const Point2D& otherP2D) const;
};
这是我的Line2D.cpp
Line2D::Line2D()
{
pt1 = Point2D();
pt2 = Point2D();
length = 0.0;
}
Line2D::Line2D(Point2D pt1, Point2D pt2):Point2D(x,y)
{
/*
// Trial 1
pt1.x = Point2D.getX();
pt1.y = Point2D.getY();
pt2.x = Point2D.getX();
pt2.y = Point2D.getY();
*/
// pt1.Point2D(x,y);
// pt2.Point2D(x,y);
// Setting for both points
// this -> pt1 = pt1;
// this -> pt2 = pt2;
// setLength();
}
Line2D::setLength()
{
// int xL = pow((pt1.x - pt2.x),2);
// int yL = pow((pt1.y - pt2.y),2);
int xL = pow((pt1.getX() - pt2.getX()),2);
int yL = pow((pt1.getY() - pt2.getY()),2);
length = sqrt(xL + yL);
this -> length = length;
}
Line2D::getScalarValue()
{
return length;
}
Line2D::getPt1()
{
return pt1;
}
Line2D::getPt2()
{
return pt2;
}
Line2D::setPt1(Point2D pt1)
{
// Setting for first point only
this -> pt1 = pt1;
}
Line2D::setPt1(Point2D pt2)
{
// Setting for second point only
this -> pt2 = pt2;
}
void Line2D::printLine2D()
{
cout << " " << pt1 << " " << pt2 << " " << length << endl;
}
bool Line2D::operator==(const Line2D& otherL2D) const
{
return(pt1 == otherL2D.pt1 &&
pt1 == otherL2D.pt2);
}
这是我的Point2D.cpp代码,
`Point2D::Point2D()
{
x = 0;
y = 0;
}
Point2D::Point2D(int x, int y)
{
this -> x = x;
this -> y = y;
}
void Point2D::setX(int x)
{
this -> x = x;
}
void Point2D::setY(int y)
{
this -> y = y;
}
int Point2D::getX()
{
return x;
}
int Point2D::getY()
{
return y;
}
void Point2D::setDistFrOrigin()
{
distFrOrigin = sqrt( pow(x,2) + pow(y,2) );
}
void Point2D::printPoint2D()
{
cout << " " << x << " " << y << " " << distFrOrigin << endl;
}
bool Point2D::operator==(const Point2D& otherP2D) const
{
return(x == otherP2D.x &&
y == otherP2D.y);
}
我的问题是:我的Point2D工作良好。Line2D封装了Point2D。我怎么写构造函数和setLength()
函数?我得到的错误是在这2:
- 在Line2D.cpp: Point2D不是Line2D的直接基础
- 在Line2D.cpp: ISO c++禁止声明没有类型 的'setLength'
- 在Line2D.cpp: setLength()不能重载(qn,我需要重载什么以及如何重载?)
- 在Line2D.cpp:调用重载的'pow(int,int)'是模棱两可的(不理解这个)
所有的include头等都是正确的,所以我没有把它们放在这里。
这些是我得到的错误。我将非常感激任何帮助。
谢谢!
1。你说的直接基地是什么意思?如果你想让他们共享相同的功能等,你需要使用抽象类,除此之外,我不确定我是否理解你的问题的这一部分。
2。你需要在函数定义前指定"void"关键字。除此之外,你的setLength()函数在你的头文件中声明为"protected"。我不知道你是否有一个特殊的理由来保护"set"函数,但通常"set"函数被声明为公开的。
3。我在头文件中看不到您的"setLength()"函数的任何其他声明,该函数稍后将被重载。你需要声明所有的函数,甚至是那些你也需要重载的函数。对于编译器来说,了解它有哪些函数是很重要的。
4。你需要更具体一些。我猜这就是你说的台词:
int xL = pow((pt1.getX() - pt2.getX()),2);
xL = (pt1's X - pt2's X) * (pt1's X - pt2's X)
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将可变参数函数的参数封装在类实例中
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 封装C++模板
- 将 RTOS 队列对象封装在仅具有静态分配的 IQueue 自定义接口中
- 从封装在对象中的函数 C++ 返回时为空的列表
- 当要访问的对象被多次封装时,如何正确使用setter
- 在为嵌套类定义行外友元时,我真的必须打破封装吗?
- 如何在类中封装C/C++套接字发送和接收函数?
- 如何使用吸气剂方法实现C++封装
- 封装 std::map 以允许迭代,但没有直接密钥访问?
- 类C++友元函数无法访问封装的类
- 当从成员类调用封装的std::begin时,程序崩溃
- 从私有成员类中断封装派生的模板类
- C++实用程序,用于将长开关语句转换为封装开关案例阶梯的简洁函数调用
- 如何修复列表视图中的错误?,封装控件时无法选择任何项
- 我们可以使用命名空间实现封装吗?
- 封装 std::vector 以允许迭代,但不允许其他内容
- lambda[=] 上的复制值被另一个封装的 lambda[&] 阻止
- 如何使用提升范围将自定义迭代器封装在函数中