type class Line没有命名类型

时间:2013-03-02 14:53:23

标签: c++ point

在分配时遇到类型问题,例如我有类型点

class point:public pair<int, int>
{
    private: int index;
    public: point(int);

    point(int, int, int);

    int GetIndex();

    float GetDiff(point);

};

point::point(int Index): index(Index) {};

point::point(int x, int y, int Index): index(Index)
{
    first = x;
    second = y; 
}

int point::GetIndex()
{
    return index;
}

float point::GetDiff(point Point)
{
     return pow(pow(Point.first-first,2.0f) + pow(Point.second-second,2.0f),0.5f);
}

编译正确,运行良好[我认为]] 但是当我想使用它时,我得到一个错误,它是使用这个类的代码(点)

class Line
{
    public:
    Line();
    point firstPoint;
    point secondPoint;
};
Line::firstPoint = point(0); // i get error, same as on line 41
//and for example

struct Minimal
{
    Minimal();
    Line line();
    void SetFirstPoint(point p)
    {
        line.firstPoint = p;//41 line, tried point(p), same error. 
        UpdateDist();
    }
    void SetSecondPoint(point p)
    {
        line.secondPoint = p;
        UpdateDist();
    }
    void UpdateDist(void)
    {
        dist = line.firstPoint.GetDiff(line.secondPoint);
    }
    float dist;
};

class Line { public: Line(); point firstPoint; point secondPoint; }; Line::firstPoint = point(0); // i get error, same as on line 41 //and for example struct Minimal { Minimal(); Line line(); void SetFirstPoint(point p) { line.firstPoint = p;//41 line, tried point(p), same error. UpdateDist(); } void SetSecondPoint(point p) { line.secondPoint = p; UpdateDist(); } void UpdateDist(void) { dist = line.firstPoint.GetDiff(line.secondPoint); } float dist; }; 哪个是给我gcc编译器的错误

1 个答案:

答案 0 :(得分:0)

请注意这一行:

Line line();

不声明类型为Line的成员变量,而是声明名为line的函数,该函数返回类型为Line的对象。因此,这段代码:

line.firstPoint = p;

意思是如下(这没什么意义,因为你会修改一个临时的):

line().firstPoint = p;

或者(最有可能)上面的声明只是意味着:

Line line; // Without parentheses

此外,您在此处收到错误的原因是:

Line::firstPoint = point(0);

firstPoint是否不是static类的Line成员变量。您首先需要Line实例,其firstPoint成员可以修改。