包括未确定的VAR错误,圆形参考C
include undeclared vars errors, circular reference C++
有两个.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/有链接静态库的说明。
相关文章:
- 使用mem_fun_ref if成员函数需要多个形参
- 通过类的模板形参特化成员模板结构
- 非类型引用形参/实参
- 哪个模板形参在boost::shared_ptr构造函数中使用一个原始指针
- 如何确保迭代器模板形参与模板类的模板形参具有相同的数据类型
- 如何在编译时通过模板形参默认值的名称/指针获取函数的类型
- c++:候选模板被忽略:模板形参显式指定的参数无效
- c++中作为形参的指针
- 哪种方法更适合为函数提供编译时间常数?函数实参与模板形参
- 包含void*结构的函数的Const正确性和形参
- 传递boost::函数,该函数接受一个模板实参作为默认为NULL的形参
- 将右值引用形参强制转换为右值引用
- 给引用形参赋值使对象无效
- 当实参是初始化列表而形参是引用时,重载解析
- const整型模板形参的条件
- 使用作为模板形参提供的基类成员,不带限定符
- 模板类的不同返回类型取决于类的形参
- 模板形参有二义性:无法推断模板实参
- 是否有必要在定义中使用模板形参来引用同一个类?
- c++ std::find()和模板形参