H.choi
H.choi

Reputation: 7

Why does this not work? (C++)

I have this code to calculate the area of shapes. I do not understand why I get the error "expected unqualified id before 'double'" and "expected ( before double" on line 44 for "class Triangle :: Triangle(double s1, double s2, double s3) : Polygon(s1, s2, s3, 0.0) {}." Any help would be very much appreciated. Thank you in advance.

    #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;
    }

Upvotes: 0

Views: 118

Answers (2)

Sultan Ahmed
Sultan Ahmed

Reputation: 2220

Use this following code snippet .

#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;
    }

Upvotes: -1

John Zwinck
John Zwinck

Reputation: 249153

You need to delete the word class here:

class Triangle :: Triangle

And of course you need to make sure class Triangle is declared before that.

Upvotes: 2

Related Questions