- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在单独的文件中正确声明和定义全局变量,并将它们包含在处理类声明的第三个文件中。
这三个文件是:
1) global.h
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
extern const int marker_num;
extern const int dim;
using namespace std;
#endif // GLOBAL_H_INCLUDED
2) global.cpp
#include <iostream>
#include <cstdio>
#include <cmath>
#include "global.h"
#include "WorldState.h"
#include "Robot.h"
#include "Sensor.h"
#include "Marker.h"
constexpr const int marker_num = 10;
constexpr const int dim = (2 * marker_num) + 3;
3) WorldState.h
#ifndef WORLDSTATE_H
#define WORLDSTATE_H
#include "global.h"
#include "global.cpp"
class WorldState{
public:
WorldState(float a[], float b[dim][dim]);
get_wstate();
protected:
private:
float w_state[];
float covar_matrix[dim][dim];
};
#endif // WORLDSTATE_H
我使用全局变量dim
来声明和定义多维数组。我已在 global.h
中声明了 dim
并在 global.cpp
中定义了它。现在,我有一个名为 WorldState
的类,在其 header 中,我使用 dim
。如果我注释掉#include "global.cpp"
,它会抛出以下错误:
C:\Users\syamp\Documents\codeblocks\slam\WorldState.h|10|error: array bound is not an integer constant before ']' token
我的理解是,包含 .h
文件也包含相应的 .cpp
,并且所有声明都应位于 .h
内所有定义都应位于 .cpp
内。然而,在这种情况下它似乎不起作用。
1) 如果我决定将 global.cpp
文件包含在 WorldState.h
中,这不是不好的编程习惯吗?我正在尝试编写一个好的代码,而不仅仅是一个可以工作的代码。
2) 另一种方法是在 global.h
内定义变量 dim
(和 marker_num
)的值。这是好的编程习惯吗?
3)我相信我缺少一些东西。请建议解决此问题的最佳方法。我正在使用 codeblocks
和 C++11
。提前致谢。
最佳答案
I am using the global variable
dim
to declare and define a multidimensional array.
在编译时声明固定长度数组时,编译器必须知道其维度的值,但是您的分隔会阻止 的值编译器根本不知道dim
,因此dim
不能用于指定固定的数组维度。任何使用 dim
的代码都会编译成对其的引用,然后链接器将在编译完成后解析引用。仅仅因为 dim
被声明为 const
并不意味着它适合作为编译时常量。为此,您必须在其声明中定义其值,例如:
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
static constexpr const int marker_num = 10;
static constexpr const int dim = (2 * marker_num);
using namespace std;
#endif // GLOBAL_H_INCLUDED
否则,如果将 dim
的声明和定义保存在单独的文件中,则必须在运行时动态分配数组,而不是在编译时静态分配。
I have declared
dim
insideglobal.h
and defined it insideglobal.cpp
.
对于运行时之前不需要使用的值来说,这很好。这不适用于您需要在编译时使用的值。
My understanding is that including the
.h
file includes the corresponding.cpp
as well
这根本不是事实。 project/makefile 在调用编译器时引入 .cpp
文件。 .h
文件与此无关。
that all declarations should be inside
.h
and all definitions should be inside.cpp
.
通常是的,但并非总是如此。
If I decide to include
global.cpp
file insideWorldState.h
, isn't it bad programming practice?
是的。
An alternative is to define values of variable(s)
dim
(andmarker_num
) insideglobal.h
. Is that good programming practice?
是的,如果您想在需要编译时常量的地方使用它们。
关于class - C++ - 在以下情况下,为什么我必须包含 .cpp 文件和/而不是 .h 文件才能访问全局变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42752144/
我是 Java 新手,这是我的代码, if( a.name == b.name && a.displayname == b.displayname && a.linknam
在下面的场景中,我有一个 bool 值。根据结果,我调用完全相同的函数,唯一的区别是参数的数量。 var myBoolean = ... if (myBoolean) { retrieve
我是一名研究 C++ 的 C 开发人员: 我是否正确理解如果我抛出异常然后堆栈将展开直到找到第一个异常处理程序?是否可以在不展开的情况下在任何 throw 上打开调试器(即不离开声明它的范围或任何更高
在修复庞大代码库中的错误时,我观察到一个奇怪的情况,其中引用的动态类型从原始 Derived 类型更改为 Base 类型!我提供了最少的代码来解释问题: struct Base { // some
我正在尝试用 C# 扩展给定的代码,但由于缺乏编程经验,我有点陷入困境。 使用 Visual Studio 社区,我尝试通过控制台读出 CPU 核心温度。该代码使用开关/外壳来查找传感器的特定名称(即
这可能是一个哲学问题。 假设您正在向页面发出 AJAX 请求(这是使用 Prototype): new Ajax.Request('target.asp', { method:"post", pa
我有以下 HTML 代码,我无法在所有浏览器中正常工作: 我试图在移动到
我对 Swift 很陌生。我如何从 addPin 函数中检索注释并能够在我的 addLocation 操作 (buttonPressed) 中使用它。我正在尝试使用压力触摸在 map 上添加图钉,在两
我设置了一个详细 View ,我是否有几个 Nib 文件根据在 Root View Controller 的表中选择的项目来加载。 我发现,对于 Nibs 的类,永远不会调用 viewDidUnloa
我需要动态访问 json 文件并使用以下代码。在本例中,“bpicsel”和“temp”是变量。最终结果类似于“data[0].extit1” var title="data["+bpicsel+"]
我需要使用第三方 WCF 服务。我已经在我的证书存储中配置了所需的证书,但是在调用 WCF 服务时出现以下异常。 向 https://XXXX.com/AHSharedServices/Custome
在几个 SO 答案(1、2)中,建议如果存在冲突则不应触发 INSERT 触发器,ON CONFLICT DO NOTHING 在触发语句中。也许我理解错了,但在我的实验中似乎并非如此。 这是我的 S
如果进行修改,则会给出org.hibernate.NonUniqueObjectException。在我的 BidderBO 类(class)中 @Override @Transactional(pr
我使用 indexOf() 方法来精细地查找数组中的对象。 直到此刻我查了一些资料,发现代码应该无法正常工作。 我在reducer中尝试了上面的代码,它成功了 let tmp = state.find
假设我有以下表格: CREATE TABLE Game ( GameID INT UNSIGNED NOT NULL, GameType TINYINT UNSIGNED NOT NU
代码: Alamofire.request(URL(string: imageUrl)!).downloadProgress(closure: { (progress) in
我是一名优秀的程序员,十分优秀!