2013-03-02 72 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