Unable to call static method in Qt

I have a simple class containing a static attribute. There are two static methods in this class: one to get the static attribute, and the other to initialize it. However, when calling the static method, the compiler reports an error.

Grade:

class Sudoku {
    Cell Grid[9][9];
    int CurrentLine;
    int CurrentColumn;

    void deleteValInColumn(int val, int col);
    void deleteValInRow(int val, int row);
    void deleteValInBox(int val, int x, int y);
    static int unsetted; //!
public:
    static void IniUnsetted() { //!
        unsetted = 0;
    }
    static int GetUns() { //!
        return unsetted;
    }
    Sudoku(ini InitGrid[9][9]);
    void Calculate_Prob_Values();
    Cell getCell(int x, int y);
    QVector<int> getPossibleValues(int x, int y);
    bool SolveIt();
};

This is the error I get:

In member function 'bool Sudoku::SolveIt()':
no return statement in function returning non-void [-Wreturn-type]
In function `ZN6Sudoku6GetUnsEv':
undefined reference to `Sudoku::unsetted` error: ld returned 1 exit status
+4
source share
4 answers

You will need to define a static variable, even if it is not initialized explicitly. This is missing in the code. You should have provided a simple example to reproduce the problem, but for your convenience I provide one that works.

main.cpp

class Foo {
    public:
        static int si;
        static void bar();
};

int Foo::si = 0; // By default, it will be initialized to zero though.

void Foo::bar() {
     Foo::si = 10;
};

int main()
{
    Foo::bar();
    return 0;
}

. , - , "unsetted" . , , , .

+4

unsetted, .

, - cpp :

int Sudoku::unsetted

, Sudoku unsetted, , .

+4

cpp ( ):

int Sudoku::unsetted = 0;
+2

- , .

:

class A
{
    public:
    static int x;    // declaration
};

int A::x;            // definition
+2

All Articles