0

There are two .hpp files

fileSystemUser.hpp

#pragma once
#include "main.h"
#include "fileCommands.hpp"//!!!Problem
#include "fileObject.hpp"
class FileSystemUser {
    ...
    void start() {
        FileCommands fc;
        ...
    }
   ....
}

fileCommands.hpp

#pragma once
#include "main.h"
#include "stringService.hpp"
#include "fileSystemUser.hpp" //!!!Problem
#include "debug.hpp"
class FileCommands {
    int analyze(string command, FileSystemUser* fileSystem) {...}
}

I build in this way:
• cmake -G "MinGW Makefiles" ..
• make //I've copied and renamed cmake-32.exe in mingw bin folder

The problem at step build after printing make: I have a lot of errors. All of them about undeclared FileSystemUser. I think that problem about includes I put at those includes //!!!Problem.

How to fix this?

Nikita
  • 1,019
  • 2
  • 15
  • 39
  • 3
    Possible duplicate of [C++ circular reference problem](http://stackoverflow.com/questions/4016471/c-circular-reference-problem) – MikeCAT Jun 26 '16 at 09:59
  • @MikeCAT I was also thinking about that, unfortunately the OP's example doesn't give a final evidence (using `#pragma once` and such). – πάντα ῥεῖ Jun 26 '16 at 10:03
  • 1
    @MikeCAT yes it was circular reference problem, I hadn't known this definition before ask. The solution: devide .hpp in .h and .cpp and use forward declorations. – Nikita Jun 26 '16 at 12:18

1 Answers1

0

This is a typical problem that named "circular reference".

In this case compiler firstly try to compile FileCommands before FileSystemUser, so the second is undeclared for the first.

To solve the issue I've done the next: Divide both .hpp at .h and .cpp and use forward declaration

//fileSystemUser.h
#pragma once
#include "main.h"
#include "fileObject.hpp"
class FileSystemUser {
    void start();
};

class FileCommands {
    int analyze(string command, FileSystemUser* fileSystem);
};

//fileSystemUser.cpp
#include "fileSystemUser.h"
void FileSystemUser::start() {
    //some code
}
//fileCommands.cpp
#include "fileSystemUser.h"
int fileCommands::analyze(string command, FileSystemUser* fileSystem) {
    //someCode
}

Another variant the .cpp and two .h

//fileSystemUser.h
#pragma once
#include "main.h"
#include "fileObject.hpp"
class FileSystemUser {
    void start();
};
#include "fileCommands.h" //after we declare the FileSystemUser 

//fileCommands.h 
#pragma once
#include "main.h"
#include "fileObject.hpp"
class FileCommands {
    int analyze(string command, FileSystemUser* fileSystem);
};

So to compile enough decalration, that why it compiles, after .cpp would compiled to static library and link, so when it links all is declared and there are no problems. https://habrahabr.ru/post/155467/ there are instructions for linking static libraries.

Nikita
  • 1,019
  • 2
  • 15
  • 39