相关疑难解决方法(0)

转发在C++中声明一个枚举

我正在尝试做类似以下的事情:

enum E;

void Foo(E e);

enum E {A, B, C};
Run Code Online (Sandbox Code Playgroud)

编译器拒绝的.我已经快速浏览了一下Google,而且共识似乎是"你做不到",但我无法理解为什么.谁能解释一下?

澄清2:我这样做是因为我在类中使用私有方法来获取枚举,并且我不希望枚举枚举值 - 例如,我不希望任何人知道E被定义为

enum E {
    FUNCTIONALITY_NORMAL, FUNCTIONALITY_RESTRICTED, FUNCTIONALITY_FOR_PROJECT_X
}
Run Code Online (Sandbox Code Playgroud)

因为项目X不是我希望用户了解的东西.

所以,我想转发声明枚举,以便我可以将私有方法放在头文件中,在cpp内部声明枚举,并将构建的库文件和标题分发给人.

至于编译器 - 它是GCC.

c++ enums

257
推荐指数
8
解决办法
18万
查看次数

枚举声明出错

我有一个非常简单的c代码:

         #include<stdio.h>
        int main()
        {
          enum boolean{true,false};
          boolean bl=false;
          if(bl==false)
             printf("This is the false value of boool\n");
         boolean bl1=true;
          if(bl1==true)
           {
            printf("This is the true value of boool\n");
           }
    return 0;
   }
Run Code Online (Sandbox Code Playgroud)

我只是尝试使用枚举类型变量.但它给出以下错误:

tryit4.c:5: error: ‘boolean’ undeclared (first use in this function)
tryit4.c:5: error: (Each undeclared identifier is reported only once
tryit4.c:5: error: for each function it appears in.)
tryit4.c:5: error: expected ‘;’ before ‘bl’
tryit4.c:6: error: ‘bl’ undeclared (first use in this function)
tryit4.c:8: error: expected ‘;’ …
Run Code Online (Sandbox Code Playgroud)

c

3
推荐指数
4
解决办法
2万
查看次数

标签 统计

c ×1

c++ ×1

enums ×1