包括未确定的VAR错误,圆形参考C

include undeclared vars errors, circular reference C++

本文关键字:形参 参考 错误 未确定 VAR 包括      更新时间:2023-10-16

有两个.hpp文件

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) {...}
}

我以这种方式构建:
•cmake -g" mingw makefiles" ..
•make//我在mingw bin文件夹中复制并重命名为cmake-32.exe

打印后逐步构建的问题:我有很多错误。所有这些都涉及未申报的FileSystemuser。我认为包括我在其中包含的问题包括//!问题。

如何解决此问题?

这是一个典型的问题,名为"圆形参考"。

在这种情况下,编译器首先尝试在Filesystemuser之前编译FileCommands,因此第二个是未申请的。

解决我下一个问题的问题:将.hpp分配为.h和.cpp并使用前向声明

//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
}

另一个变体.cpp和两个.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);
};

因此,要编译足够的脱位,以便为何汇编.cpp将其编译为静态库和链接,因此,当它链接时,所有内容都会被声明,没有问题。https://habrahabr.ru/post/155467/有链接静态库的说明。