0

Tower of Hanoi Problem: // { Driver Code Starts // Initial Template for C++

#include <bits/stdc++.h>
using namespace std;

 // } Driver Code Ends
// User function Template for C++

class Solution{
public:
    static int i=0;
    void hanoi(vector<int>& v,int N,int n,int start,int end)
    {
        i++;
        if(i==n)
        {
            v[0]=start;
            v[1]=end;
            return;
        }
        int other=6-(start+end);
        hanoi(v,N-1,n,start,other);
        hanoi(v,N-1,n,other,end);
    }
    vector<int> shiftPile(int N, int n){
        // code here
        Solution::i=0;
        vector<int>v(2,0);
        hanoi(v,N,n,1,3);
        return v;
    }
};

// { Driver Code Starts.

int main(){
    int t;
    cin>>t;
    while(t--){
        int N, n;
        cin>>N>>n;
        
        Solution ob;
        vector<int> ans = ob.shiftPile(N, n);
        cout<<ans[0]<<" "<<ans[1]<<endl;
    }
    return 0;
}  // } Driver Code Ends

The Error is coming in the class Solution that I cannot declare it [C++ forbids in-class initialization of non-const static member Solution::i].Please help to tackle the above problem.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Raja Sethi
  • 11
  • 3

1 Answers1

0

Declare and define the static data member like

class Solution{
public:
    static int i;
//...
};

int Solution::i = 0;

Though it is unclear why the variable i is declared with the storage-class specifier static.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335