0

I'm building a simple application to solve sudoku puzzles. It's my first time creating something serious with C++, so I'm open for code style/structure critique.
The problem I bumped into has something to do with organizing multiple files.
I have two classes referencing each other using functions. When I try to call a function:

void Field::runColumnCheckout(CellGroup* sender, int cellRelativeX)
{

}

in a CellGroup class using an instance of 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;
        }
    }

    //...
}

An undefined reference occures. I don't quite understand, why.
All the examples were taken from cell_group.cpp and field.cpp that are defining classes from cell_group.h and field.h.

Unfortunately, I couldn't manage to put all the files in a question as they have gained a lot of lines, but you can look at them on my github.

I have found a similar question and another one, but they seem to have issue with the way they compile their files. I've had two referencing each other classes structured similarly before adding code I have problem with right now and everything compiled fine.

I'm compiling everything using GCC compiler on Windows and CodeBlocks as an IDE.

  • You mostly have done it correctly. -> forward declaration. just remove the unneeded (and so problematic ) `#include` from header files. – Jarod42 Feb 13 '19 at 09:41
  • For MCVE, you don't need to post all code, just reduce to minimal, so in your case empty classes with pointer usage of other class + `#include` . – Jarod42 Feb 13 '19 at 09:44
  • Odds are, `Field::runColumnCheckout()` is defined (implemented) in a source file, that is either not compiled, or its object file is not supplied to the linker. Hard to be sure though, as you haven't provided enough information. Look at your commands for compiling (what source files are listed?) and - if you are linking separately - the command for linking (what object files are listed?). You will find the relevant file missing from one or both of those commands. – Peter Feb 13 '19 at 09:47
  • @Peter the field.o file is missing. How can I add it manually and why CodeBlocks haven't generated it? – Viktor Scherbakov Feb 13 '19 at 09:51
  • @ViktorScherbakov - without knowing how you set up your project, it's hard to be definitive (plenty of things you could have done to cause this). But, try adding the source file (field.cpp or field.cc, depending on how you named it) to the project. Easy way to do that is through the menu Project->Add Files ... which brings up a dialog to search for files. I would probably do a Build-Clean then a Rebuild All after that, to give the IDE a chance to sort itself out. – Peter Feb 13 '19 at 10:49
  • @Peter this solved it. Thanks! – Viktor Scherbakov Feb 13 '19 at 13:41

1 Answers1

0

CodeBlocks somehow lost connection with field.cpp file or didn't add it creating file. Going and manually adding it to the project tree (even though it was already there) Project->Add files... solved the problem. Thanks @Peter for finding the solution.