2015-01-01 57 views
-4

得到錯誤 幫助需要調用功能從結構

struct Point { 
     double x , y ; 
     double del ; 
     Point() {} 
     Point (double _x , double _y){ x = _x; y = _y;} 

     double Dist(const Point &P){ double dx = x - P.x ; double dy = y - P.y; return sqrt(dx*dx + dy *dy);} 

}; 

int ccw (Point p,Point q ,Point r) 
{ 

     double val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); 


     if(fabs(val) < EPS) return 0; 
     if(val > EPS) return 1; 
     else return 2 ; 

} 

struct seg2 { 
     Point a, b; 
     seg2(){} 
     seg2(Point _a ,Point _b){a = _a;b= _b;} 
     double length(){ return a.Dist(b);} 
     bool onseg(const Point &p) { 
       return (a.Dist(p) + b.Dist(p) - length() < 0); 
     } 
     bool isIntersect (const seg2& S){ 
       int o1 = ccw(a, b, S.a); 
       int o2 = ccw(a, b, S.b); 
       int o3 = ccw(S.a, S.b, a); 
       int o4 = ccw(S.a, S.b, b); 

       if(o1 != o2 && o3 != o4){ 
         return true; 
       } 
       if(o1 == 0 && onseg(S.a)) return true; 
       if(o2 == 0 && onseg(S.b)) return true; 

       if(o3 == 0 && S.onseg(a)) return true; // Here I want to call S.onseg 
       if(o4 == 0 && S.onseg(b)) return true; // Here 
       return false; 
     } 


}; 

我給出的這個錯誤

member function 'onseg' not viable: 'this' argument has type 'const seg2', 
     but function is not marked const 
       if(o3 == 0 && S.onseg(a)) return true; 

'onseg' declared here 
     bool onseg(const Point &p) { 

member function 'onseg' not viable: 'this' argument has type 'const seg2', 
     but function is not marked const 
       if(o4 == 0 && S.onseg(b)) return true; 

'onseg' declared here 
     bool onseg(const Point &p) { 

這是一個程序,兩個線段相交或不 但我不能調用onseg函數從結構 :( C++代碼.. 這給我編譯錯誤.. 我該怎麼打電話?

+4

您至少需要給我們看到的錯誤 –

+0

@ Matrix.code [在您的問題當然!](http://stackoverflow.com/posts/27733184/edit) –

回答

0
bool isIntersect (const seg2& S){ 

S被聲明爲const,然後你正在嘗試調用該non-const功能: -

S.onseg(a); <<<< `non-const` function. 

更改您的功能定義: -

bool onseg(const Point &p) const    <<<<<<<<< const added 
{ 
    return (a.Dist(p) + b.Dist(p) - length() < 0); 
} 

const以及如non-const對象調用這個。

+0

有什麼有用的理由使用const? 如果不是,那麼我可以不使用常量參數來聲明函數 –

+0

如果不會更改數據成員,那麼使函數常量總是更好...您可以通過使用常量正確性來了解更多信息。 – ravi