包括未声明的变量错误,循环引用 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
打印make后step build的问题:
我有很多错误。所有这些都是关于未声明的 FileSystemUser。我认为关于 includes 的问题我放在那些 includes //!!!Problem.
如何解决这个问题?
这是一个名为"circular reference"的典型问题。
在这种情况下,编译器首先尝试在 FileSystemUser 之前编译 FileCommands,因此第二个未声明为第一个。
为了解决这个问题,我做了以下操作:
在 .h 和 .cpp 处划分 .hpp 并使用前向声明
//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);
};
所以要编译足够的 decalration,这就是编译的原因,在 .cpp 将编译为静态库和 link 之后,所以当它 link 全部被声明并且没有问题时。 https://habrahabr.ru/post/155467/ 有关于 linking 静态库的说明。
有两个 .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文件夹
打印make后step build的问题: 我有很多错误。所有这些都是关于未声明的 FileSystemUser。我认为关于 includes 的问题我放在那些 includes //!!!Problem.
如何解决这个问题?
这是一个名为"circular reference"的典型问题。
在这种情况下,编译器首先尝试在 FileSystemUser 之前编译 FileCommands,因此第二个未声明为第一个。
为了解决这个问题,我做了以下操作: 在 .h 和 .cpp 处划分 .hpp 并使用前向声明
//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);
};
所以要编译足够的 decalration,这就是编译的原因,在 .cpp 将编译为静态库和 link 之后,所以当它 link 全部被声明并且没有问题时。 https://habrahabr.ru/post/155467/ 有关于 linking 静态库的说明。