“ PointType”不是“ Point”的成员

时间:2019-05-25 01:42:09

标签: c++

我收到一个编译错误,提示“'PointType'不是'Point'的成员”,我不知道我需要在代码中实现什么。现在想了一段时间。我试过几次在这里和那里调整代码。但是没有想到一个解决方案。我对“ Point point(Point :: PointType(x,y),depth);”感到困惑在main()中想要。 PointType(x,y)到底是什么?谁能启发我该怎么做?在此感谢任何人的帮助。注意:Main()无法被触摸。谢谢!

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

struct PointType
{
float x;
float y;
PointType(const float x1, const float y1) :x(x1),y(y1){}
};

class Object
{
private:
float d;
PointType * pt;
public:
Object(float n) : d(n){}

 float depth() const
{
    return d;
}
};

class Point :public Object
{
private:
PointType mpoint;

public:

Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}

virtual ~Point();

 };

主文件:

const float EPSILON = 1e-5f;

bool is_near(float x, float y)
{
return std::abs(x - y) < EPSILON;
}
float frand()
{
return 10.0f * float(rand()) / float(RAND_MAX);
}

int main()
{

srand(unsigned(time(0)));
int count = 0;
int max_count = 0;

float x = frand();
float y = frand();
float depth = frand();

Point point(Point::PointType(x, y), depth);
    if (is_near(point.depth(), depth))
{
    ++count;
}
else
{
    std::cout << "  - Point::depth test failed" << std::endl;
}
++max_count;


 }

1 个答案:

答案 0 :(得分:0)

您已经设置了一组循环声明,如果您无法更改main(),则无法解析。您目前在PointType类之外将struct作为Point。很好,但是您需要将main()中的行更改为:

Point point(Point::PointType(x, y), depth);

收件人:

Point point(PointType(x, y), depth);

但是,如果您无法执行此操作,则将无法编译它,因为PointObject的子类,但是Object需要Point::PointType尚未定义。为了让PointType拥有Point,您需要这样声明:

class Point :public Object
{
    struct PointType
    {
        float x;
        float y;
        PointType(const float x1, const float y1) :x(x1),y(y1){}
    };
private:
    PointType mpoint;

public:

    Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}

    virtual ~Point();

 };

但是一旦将PointType放入Point,就不能声明Object的任何成员都具有Point::PointType类型,因为Point继承自Object

您可以将Object::pt声明为void*,但这会丢弃危险的类型信息。