0

This program may look like foolish program but i want to know the solution for it

#include <iostream>

using namespace std;

class base
{
    public:
     static int i;
     static int &j = i;
};

int base::i=9;

int main()
{

    cout<<base::j<<" "<<base::i;

    return 0;
}

when i am compiling this program i am getting the error "const expression needed for static class initialization" can anyone help me thanks in advance

1 Answer 1

3

Simple: move it out of class as well:

#include <iostream>

using namespace std;

class base
{
    public:
     static int i;
     static int &j;
};

int base::i=9;
int &base::j=base::i;

int main()
{

    cout<<base::j<<" "<<base::i;

    return 0;
}

Live example

Sign up to request clarification or add additional context in comments.

1 Comment

@user2413497 If it works for you, you can accept the answer (green tickmark). That's how SO works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.