Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Getting error error: \'constexpr\' needed for in-class initialization of static

ID: 3907377 • Letter: G

Question

Getting error error: 'constexpr' needed for in-class initialization of static data member 'const double Circle::PI' of non-integral type [-fpermissive] for code below. Please help.

#include "circle.h"

#include "square.h"

#include <iostream>

#include <cstdio>

#include <cstdlib>

using namespace std;

int main()

{

    char type;

    while(true)

    {

        cout << "Enter type of figure(C - Circle, S - Square or Q - quit: "; cin >> type;

        if(type == 'C')

        {

            Circle* c = new Circle;

            c->input();

            c->print();

            delete c;

        }

        else if(type == 'S')

        {

            Square* s = new Square;

            s->input();

            s->print();

            delete s;

        }

        else if(type == 'Q')

            exit(0);

        else

            cout << "Invalid ";

        cout << endl;

    }

}

circle.h

#ifndef CIRCLE_H

#define CIRCLE_H

#include <iostream>

class Circle

{

    double radius;

    //constexpr static const double PI = 3.14;

    static const double PI = 3.14;

public:

    Circle(double r = .0);

    void print() const;

    double area() const;

    void input();

};

#endif // CIRCLE_H

Square.H

#ifndef SQUARE_H_INCLUDED

#define SQUARE_H_INCLUDED

#include <iostream>

class Square

{

private:

    double a;

    double b;

public:

    Square(double _a = 0.0, double _b = 0.0);

    void    print() const;

    double area() const;

    void    input();

};

#endif // SQUARE_H_INCLUDED

Explanation / Answer

use -std=c++11 while compiling

Example: g++ mainDemo.cpp circle.cpp shape.cpp -std=c++11

Let me know still facing error, Thank you...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote