Array in a class

Hi! :)
I'm learning to use classes in C++, but I just met an issue.
So basically I tried to write a class named "Triangle" containing thingummies like area, circumference, angles and so on.

The area and circumference work very well, but I have an issue with the angles : most of the time I get a "nan" :/
I re-read it like ten times but I can't find my mistake...
Would someone have an idea about it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    class Triangle {
        float sides[3];
    public:
        float angles[3] = {workoutangle(sides[0], sides[1], sides[2]),workoutangle(sides[1], sides[2], sides[0]),workoutangle(sides[2], sides[0], sides[1])};
        void set_dimension () {cout << "Side 1 : "; cin >> sides[0]; cout << "Side 2 : "; cin >> sides[1]; cout << "Side 3 : "; cin >> sides[2];}
        float area() {return sqrt(hperimeter()*(hperimeter()-sides[0])*(hperimeter()-sides[1])*(hperimeter()-sides[2])); }
        float circumference() {return 2*hperimeter(); }
        void angle() {cout << "Angles (disp. in rad): " << angles[0] << ", " << angles[1] << ", " << angles[2] << endl;}
    private:
        float hperimeter() {return (sides[0]+sides[1]+sides[2])/2; }
        float workoutangle(float sideL, float sideR, float sideO) {return acos((pow(sideR, 2)+pow(sideL, 2)-pow(sideO, 2))/2*sideR*sideL); }
        };

    class Circle {
        float radius;
    public:
        void set_dimension() {cout << "Radius : "; cin >> radius;}
        };

    Triangle babytriangle;
    babytriangle.set_dimension();
    babytriangle.angle();
    cout << "Circonference: " << babytriangle.circumference() << endl;
    cout << "Area: " << babytriangle.area() << endl;

    return 0;


While compiling, the build log also reports two warnings which I don't understand pretty well... They are both for line 11 (in bold in the code).
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]

First of all, why would it say they are non-static initializers? Because I am using a function in their declarations?
Subsequently, is {1,1,2} an extended initializer list? If so why does this warning never show up to me when I use it usually? I mean like in "int threenumbers[3] = {1,2,3};"?

Anyway, thanks a bunch for your time and consideration.
TGT
Last edited on
hiya,

Defining a class inside main is very wrong.

move lines 8 - 25 in front of your main().

you cant initialise your angles array with data you do not know yet. You need an instance of a Triangle object to call workoutangle, and your sides arrays will have nothing in it.
Last edited on
Topic archived. No new replies allowed.