0

割り当て中にタイプに問題があります。たとえば、タイプポイントがあります

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;
};

そして、gccコンパイラを与えるエラーはどこにありますか

|41|error: 'firstPoint' in 'class Line' does not name a type|
4

1 に答える 1

0

この行に注意してください:

Line line();

型のメンバー変数を宣言するのではなく、型のオブジェクトを返すLine呼び出される関数を宣言します。したがって、次のいずれかのコード:lineLine

line.firstPoint = p;

次のようになっています(一時的に変更するため、ほとんど意味がありません)。

line().firstPoint = p;

または(ほとんどの場合)上記の宣言は、次のように意図されています。

Line line; // Without parentheses

さらに、ここでエラーが発生する理由:

Line::firstPoint = point(0);

それはクラスのメンバー変数でfirstPointはありませんか。最初に、変更できるメンバーのインスタンスが必要です。staticLineLinefirstPoint

于 2013-03-02T14:56:02.610 に答える