代码之家  ›  专栏  ›  技术社区  ›  goulashsoup

当在类中使用全局时,C++不是“类或名称空间”

  •  -1
  • goulashsoup  · 技术社区  · 6 年前

    我有一个名称空间 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 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公司 全局::isLittleEndian 存在,它有什么价值?

    1 回复  |  直到 6 年前
        1
  •  -1
  •   goulashsoup    6 年前

    我创造了一个 Endianness.h 定义预处理器宏的文件 IS_LITTLE_ENDIAN

    #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 是小恩迪亚吗 .