"ISO C++ forbids initialization of member" error

I get the following error when i compile:
ISO C++ forbids initialization of member ‘numIterations’

making ‘numIterations’ static

ISO C++ forbids in-class initialization of non-const static member ‘numIterations’


This occurs for searchStatus, xStop fStop, and numIterations .

Here is the code for the file containing this error:
SolutionNLE.h

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
34
#ifndef SOLUTIONNLE_H
#define	SOLUTIONNLE_H

namespace csc350Lib_calculus_snle
{
    typedef enum SolutionStatus
    {
        SEARCH_SUCCESSFUL = 0,
                SEARCH_FAILED_TOO_MANY_ITERATIONS = 1,
                SEARCH_FAILED_OUT_OF_RANGE = 2,
                SEARCH_FAILED_NUMERICAL_ERROR = 3,
                SEARCH_FAILED_OTHER_REASON = 4
    }
    SolutionStatus;
    
    class SolutionNLE
    {
        SolutionStatus searchStatus;
        float xStop = 0.f;
        float fStop = 0.f;
        int numIterations = 0;
    public:
        SolutionNLE(void);
        virtual ~SolutionNLE(void);
        SolutionStatus getStatus(void); //This method returns the search's status.
        float getSolution(void); //This method returns the value of the solution estimate computed. This value makes sense when the search has been successful and also to some extent for a search that stopped after too many iterations.
        float getValueAtSolution(void); //This method returns the value of the function at the solution estimate computed.
        int getNumberOfIterations(void); //This method returns the number of iterations when the search stopped.
    private:
    };
}

#endif	/* SOLUTIONNLE_H */

SolutionNLE.cpp
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
#include "SolutionNLE.h"

using namespace csc350Lib_calculus_snle;

SolutionNLE::SolutionNLE(void)
{
}

SolutionNLE::~SolutionNLE(void)
{
}

SolutionStatus getStatus(void)
{
    return searchStatus;
}
float SolutionNLE::getSolution(void)
{
    return xStop;
}

float SolutionNLE::getValueAtSolution(void)
{
    return fStop;
}

int SolutionNLE::getNumberOfIterations(void)
{
    return numIterations;
}


Any help would be much appreciated it.
Initialize your members in the constructor, preferably in its initialization list. Only static const integral members can be initialized inside the class definition like that.
Topic archived. No new replies allowed.