2016-12-27 17 views
0
/** Computes the area of this triangle. @return This triangle's area. */ 
template<class T> 
T Triangle<T>::getArea() const { 
    double s, area; 
    s= (side1+side2+side3)/2; 
    area = sqrt(s*(s-side1) * (s-side2)* (s-side3)); 
    return area; 

} 

預期的輸出是9。我使用谷歌測試。的功能總是輸出了錯誤的區域

回答

1

我可以看到在你的代碼非常簡單的錯誤。當你宣佈你的double變量,你也需要浮點申報數量。所以對於你的代碼,你只需要在0之後加零(.0)。

s =(side1 +side2+side3)/2.0; 
+0

哈哈是啊,我怎麼犯這樣的錯誤。感謝您的支持。 – suman