2013-03-02 70 views
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| 
+0

Line line();是一種方法而不是一個對象。在第41行,你試圖像使用對象一樣使用它。 – haitaka 2013-03-02 14:56:38

回答

0

意識到錯誤這條線:

Line line(); 

不聲明Line類型的成員變量,而是一個函數調用line返回Line類型的對象。因此,無論此代碼:

line.firstPoint = p; 

,就是要如下(這將毫無意義,因爲你會被修改臨時):

line().firstPoint = p; 

或(最有可能)上面的聲明只是意思是:

Line line; // Without parentheses 

而且,爲什麼你這裏的錯誤的原因:

Line::firstPoint = point(0); 

firstPoint是不是類別Line的成員變量。您首先需要實例Line,其firstPoint成員可以修改。

+0

好的)謝謝,它的工作原理)但我現在:最小rez;我得到:未定義的參考'Minimal :: Minimal()'|,如何解決它? – john 2013-03-02 15:12:42

+0

@john:1.提供一個定義(你只是聲明它); 2.我認爲你應該至少閱讀一本關於C++或者一些教程的介紹性書籍:-) – 2013-03-02 15:17:57

+0

))你能推薦一本好書嗎?) – john 2013-03-02 16:02:43