为什么我得到这个错误的封装在c++

Why am I getting this error on encapsulation in C++?

本文关键字:封装 c++ 错误 为什么      更新时间:2023-10-16

这是我的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:

  1. 在Line2D.cpp: Point2D不是Line2D的直接基础
  2. 在Line2D.cpp: ISO c++禁止声明没有类型
  3. 的'setLength'
  4. 在Line2D.cpp: setLength()不能重载(qn,我需要重载什么以及如何重载?)
  5. 在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)