gpt4 book ai didi

c++ - Blitz++ 数组作为全局数组

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:00 24 4
gpt4 key购买 nike

我正在用 C++ 开发一个具有许多功能的项目。我不想在主程序中编写它们,而是想为每个函数编写一个单独的 .cpp 文件。这些函数中的大多数都会作用于一些数组,所以我希望将这些数组设为全局。所以我在一个名为 globals.cpp 的单独 .cpp 文件中声明了所有数组,并将它们放在一个以 extern 为前缀的 globals.h 文件中。我像往常一样编写函数和主程序,但是当我编译时,我得到一个

这是我的:

//全局变量.cpp

#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"

BZ_USING_NAMESPACE(blitz)

Array<double,2> A(5,5);

在我的globals.h文件中

#ifndef GLOBALS_H
#define GLOBALS_H
extern Array<double,2> A(5,5);
#endif

然后我有一个函数add.cpp,例如

#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"

BZ_USING_NAMESPACE(blitz)

void add.cpp(){
for(int i=0;i<5;i++){
A(i,i)=i*i;
}
}

我明明把它包含在了prototype.h文件中

#ifndef GLOBALS_H
#define GLOBALS_H
void add();
#endif

终于有了主程序mainprog.c

#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"

BZ_USING_NAMESPACE(blitz)
int main(){
add();
cout<<A<<endl;
return 0;
}

然而,当我编译时出现错误 `globals.h:6:8: error: ‘Array’ does not name a type

然后 add.cpp 函数中出现错误,指出错误 A 未声明。

如何将 blitz 数组声明为全局数组?谢谢`

最佳答案

问题是您导入命名空间的宏 (BZ_USING_NAMESPACE) 低于您对 globals.h 的包含。因此,您尝试在 globals.h 中引用的 Array 类实际上是 blitz::Array 或此时的其他内容。

对于一个简单的修复,只需使用 globals.h 中 A 声明正上方的 BZ_USING_NAMESPACE。

永远记得在该头文件中包含头文件所需的所有内容。

#ifndef GLOBALS_H
#define GLOBALS_H

#include <blitz/blitz.h> //No idea if the Array class needs this header.
#include <blitz/array.h>
BZ_USING_NAMESPACE(blitz)

extern Array<double,2> A(5,5);
#endif

关于c++ - Blitz++ 数组作为全局数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10666618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com