C++ - 尝试在类中使用全局时'Not a class or namespace'
C++ - 'Not a class or namespace' when try to use global in class
我有一个命名空间Global
,在我的主函数之前有一个常量静态变量:
#include "RaGaCCMainView.h"
#include <QApplication>
namespace Global {
const static bool isLittleEndian = [](){
union {
uint32_t i;
char c[4];
} bint = {0x01020304};
return bint.c[0] == 1;
}();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
RaGaCCMainView w;
w.setAttribute(Qt::WA_QuitOnClose);
w.show();
return a.exec();
}
现在在RaGaCCMainView.h
中,我想将变量声明为 extern:
extern const static bool Global::isLittleEndian;
这是我得到相应错误的地方:
C2653: 'Global': is not a class or namespace name
我只想在RaGaCCMainView.cpp
中使用全局变量:
void RaGaCCMainView::someFunction()
{
...
if(Global::isLittleEndian) {
...
}
}
这似乎是一个愚蠢的问题,但我唯一能回答的答案没有帮助或不起作用。我(显然(想声明和定义一次Global::isLittleEndian
,然后在这种情况下在我想要的任何类中使用它RaGaCCMainView
.
如何使RaGaCCMainView
知道Global::isLittleEndian
存在以及它有什么价值?
我创建了一个定义预处理器宏IS_LITTLE_ENDIAN
的Endianness.h
文件:
#ifndef ENDIANNESS_H
#define ENDIANNESS_H
#include <QtGlobal>
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
#define IS_LITTLE_ENDIAN 1
#else
#define IS_LITTLE_ENDIAN 0
#endif
#endif // ENDIANNESS_H
我将这个包含在类RaGaCCMainView
中并使用宏IS_LITTLE_ENDIAN
.
相关文章:
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- "using namespace std;"在C++的作用是什么?
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- OpenCV Android C++ imwrite not found
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 错误"Could not find Boost"(缺少:上下文标头)
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- Is !NaN not a NaN?
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- C++ - 尝试在类中使用全局时'Not a class or namespace'
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 什么原因导致我的"‘FatalError’ in namespace ‘Foam’ does not name a type"错误?
- 在DX11应用程序中使用DirectXHelper.h会导致Visual Studio 2015中出现'namespace/class not found'错误
- " a namespace name is not allowed" javacpp 中的错误
- "error C2653: System is not a class or a namespace name"在窗体头文件、可视C++
- 命名空间错误'Not a namespace-name'
- 使用嵌套命名空间声明"using namespace"时出错 ( "namespace xxx::yyy not allowed in using-declaration" )