未定义的函数引用是另一个 class 的成员
Undefined reference to function which is member of an another class
我正在构建一个简单的应用程序来解决数独难题。这是我第一次使用 C++ 创建一些严肃的东西,所以我愿意接受代码 style/structure 批评。
我遇到的问题与组织多个文件有关。
我有两个 classes 使用函数相互引用。当我尝试调用函数时:
void Field::runColumnCheckout(CellGroup* sender, int cellRelativeX)
{
}
在 CellGroup
class 中使用 Field
class:
的实例
void CellGroup::runISC(int possibilityNumber)
{
for (int x = 0; x < 3; x++) {
int amountInColumn = 0;
for (int y = 0; y < 3; y++)
if (cells[x][y]->isPossible(possibilityNumber))
amountInColumn++;
if (amountInColumn > 1) {
//parentField is an instance of a Field class
//stored in a private field of the CellGroup class
parentField->runColumnCheckout(this, x);
return;
}
}
//...
}
出现未定义的引用。不太明白,为什么。
所有示例均取自 cell_group.cpp
和 field.cpp
,它们定义了 cell_group.h
和 field.h
.
中的 classes
不幸的是,我无法将所有文件都放在一个问题中,因为它们已经增加了很多行,但您可以在我的 github.
上查看它们
我找到了 similar question and another one,但他们编译文件的方式似乎有问题。在添加我现在遇到问题的代码之前,我有两个相互引用 class 结构相似,并且一切都编译正常。
我在 Windows 上使用 GCC 编译器编译所有内容,并将 CodeBlocks 作为 IDE。
CodeBlocks 以某种方式失去了与 field.cpp 文件的连接,或者没有添加它来创建文件。将其手动添加到项目树中(即使它已经存在)项目->添加文件...解决了问题。
感谢@Peter 找到解决方案。
我正在构建一个简单的应用程序来解决数独难题。这是我第一次使用 C++ 创建一些严肃的东西,所以我愿意接受代码 style/structure 批评。
我遇到的问题与组织多个文件有关。
我有两个 classes 使用函数相互引用。当我尝试调用函数时:
void Field::runColumnCheckout(CellGroup* sender, int cellRelativeX)
{
}
在 CellGroup
class 中使用 Field
class:
void CellGroup::runISC(int possibilityNumber)
{
for (int x = 0; x < 3; x++) {
int amountInColumn = 0;
for (int y = 0; y < 3; y++)
if (cells[x][y]->isPossible(possibilityNumber))
amountInColumn++;
if (amountInColumn > 1) {
//parentField is an instance of a Field class
//stored in a private field of the CellGroup class
parentField->runColumnCheckout(this, x);
return;
}
}
//...
}
出现未定义的引用。不太明白,为什么。
所有示例均取自 cell_group.cpp
和 field.cpp
,它们定义了 cell_group.h
和 field.h
.
不幸的是,我无法将所有文件都放在一个问题中,因为它们已经增加了很多行,但您可以在我的 github.
上查看它们我找到了 similar question and another one,但他们编译文件的方式似乎有问题。在添加我现在遇到问题的代码之前,我有两个相互引用 class 结构相似,并且一切都编译正常。
我在 Windows 上使用 GCC 编译器编译所有内容,并将 CodeBlocks 作为 IDE。
CodeBlocks 以某种方式失去了与 field.cpp 文件的连接,或者没有添加它来创建文件。将其手动添加到项目树中(即使它已经存在)项目->添加文件...解决了问题。 感谢@Peter 找到解决方案。