Ous*_*aki 5 c++ qt static class
我有一个包含静态属性的简单类。这个类中有两个静态方法:一个获取静态属性,另一个初始化它。然而,当调用静态方法时,编译器会报告错误。
班上:
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();
};
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
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
Run Code Online (Sandbox Code Playgroud)
您将需要定义静态变量,即使它没有显式初始化。这就是您的代码中缺少的内容。您应该提供一个简单的示例来重现该问题,但为了您的方便,我提供了一个有效的示例。
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;
}
Run Code Online (Sandbox Code Playgroud)
注意:我建议让某人检查您的代码,因为“未设置”是不正确的英语。如果我们这样做,您可能还需要修复缩进。
在您的代码中没有 的定义unsetted
,只有声明。
解决方案是在您的 cpp 文件中放置如下一行:
int Sudoku::unsetted
Run Code Online (Sandbox Code Playgroud)
这样做的原因是Sudoku
类的每个实例化都将使用相同的unsetted
成员,因此无法为每个实例定义它,因此程序员只能在一个地方定义它。