2017-05-14 81 views
0

我有這段代碼來計算形狀的面積。我不明白爲什麼我得到的錯誤「預期不合格的ID之前‘雙’」和「預期(前雙」關於「類三角::三角(雙S1,S2雙,雙S3)線44:多邊形(S1, S2,S3,0.0){}「任何幫助將是非常讚賞謝謝你在前進爲什麼這不起作用? (C++)

#include "Polygon.h" 
    #include <iostream> 
    #include <cmath> 
    using namespace std; 

    //Polygon constructor 
    Polygon :: Polygon(double side1, double side2, double side3, double side4) 
    { 
     s1 = side1; 
     s2 = side2; 
     s3 = side3; 
     s4 = side4; 
    } 
    //get area method 
    double Polygon :: getArea() 
    { 
     float length, width, area; 

     if (s1 == s2) 
      { 
       length = s1; 
       width = s3; 
      } 
     else if (s1 == s3) 
      { 
       length = s1; 
       width = s2; 
      } 
     else if (s1 == s4) 
      { 
       length = s1; 
       width = s3; 
      } 
     area = length * width; 
     return area; 
    } 
    //Triangle class 
    class Triangle :: Triangle(double s1, double s2, double s3) : Polygon(s1,  s2, s3, 0.0) {} 

    double Triangle :: getArea() 
    { 
     float s, area; 

      s = (s1 + s2 + s3)/2; 
      area = sqrt(s * (s - s1) * (s - s2) * (s - s3)); 

      return area; 
    } 
+1

http://en.cppreference.com/w/cpp/language/class類聲明... –

回答

2

您需要刪除單詞class這裏:。

class Triangle :: Triangle 

當然你需要確保class Triangle是之前宣佈。

+0

它現在可以工作!非常感謝!! –

+2

原因'class'在這裏製造麻煩,就是它作爲解析類'(三角::三角)''不(類三角形):: Triangle'。而「三角形::三角形」不是一個類。 –

+0

@BenVoigt既不那些將是有效的(即使沒有括號),相關的語法是*合格-ID *和被定義爲*嵌套名說明符* template_opt *不合格-ID *;和*闡述型說明符*不能是*嵌套名稱說明符* –

-1

使用此弗洛翼代碼片段。

#include "Polygon.h" 
    #include <iostream> 
    #include <cmath> 
    using namespace std; 

    //Polygon constructor 
    Polygon :: Polygon(double side1, double side2, double side3, double side4) 
    { 
     s1 = side1; 
     s2 = side2; 
     s3 = side3; 
     s4 = side4; 
    } 
    //get area method 
    double Polygon :: getArea() 
    { 
     float length, width, area; 

     if (s1 == s2) 
      { 
       length = s1; 
       width = s3; 
      } 
     else if (s1 == s3) 
      { 
       length = s1; 
       width = s2; 
      } 
     else if (s1 == s4) 
      { 
       length = s1; 
       width = s3; 
      } 
     area = length * width; 
     return area; 
    } 
    //Triangle class 
    Triangle :: Triangle(double s1, double s2, double s3) : Polygon(s1,  s2, s3, 0.0) {} 

    double Triangle :: getArea() 
    { 
     float s, area; 

      s = (s1 + s2 + s3)/2; 
      area = sqrt(s * (s - s1) * (s - s2) * (s - s3)); 

      return area; 
    } 
+3

的一部分,不要只轉儲代碼沒有說明文字。 –