c - 在结构中的函数中传递枚举(在 C 中)

标签 c function enums struct argument-passing

我查看了如何在函数中传递枚举,但是当函数和枚举都在结构中声明时,该方法不起作用。这是我的代码:

测试设置.h:

     1 #ifndef TEST_SETUP_H_
     2 #define TEST_SETUP_H_
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 
     6 
     7 typedef struct _test_setup {
     8 
     9   int *partner;            
    10   int *send_first;         
    11   double *results;         
    12 
    13   enum { CHIP_WIDE, NODE_WIDE, SYSTEM_WIDE } SomeMatches;
    14   void match_partners(SomeMatches match);
    15 
    16 } test_setup;              
    17 
    18 #endif

测试设置.c :

     1 #include "../includes/test_setup.h"
     2 
     3 void match_partners(SomeMatches match) { 
     4   if (match == CHIP_WIDE) {
     5 
     6   }
     7   else if (match == NODE_WIDE) {
     8 
     9   }
    10   else if (match == SYSTEM_WIDE) {
    11 
    12   }
    13   else {
    14 
    15   }
    16 }`

错误:

    In file included from src/test_setup.c:1:
    src/../includes/test_setup.h:14: error: expected ‘)’ before ‘match’
    src/../includes/test_setup.h:16: warning: no semicolon at end of struct or union
    src/test_setup.c:3: error: expected ‘)’ before ‘match’
    make: *** [setup.o] Error 1

我已经尝试了声明枚举和在函数参数中使用它的所有组合,但没有任何效果。任何想法将不胜感激。我正在使用 mpicc 进行编译(因为程序的其余部分使用了 MPI 函数)但是我已经尝试使用 GNU GCC 并且我得到了相同的警告/错误。

最佳答案

对于 C

如果你真的想要 C,那么你根本做不到这些。

  • 不能在结构体中定义成员函数
  • 您不能定义嵌套在结构中的命名枚举

对于 C++

使用范围解析运算符::

#include<iostream>

struct Test
{
  enum SomeEnum { TEST1, TEST2, TEST3 };
  void SomeFunction(SomeEnum e);
};

void Test::SomeFunction(Test::SomeEnum e)
{
  std::cout << e << "\n";
}

int main(int argc, char* argv[])
{
  Test t;
  t.SomeFunction(Test::TEST1);
  return 0;
}

关于c - 在结构中的函数中传递枚举(在 C 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6460639/

相关文章:

java - 枚举的 values() 方法访问级别

java - Spring MVC - 将枚举填充到下拉列表

function - 从 React-Native ListView 行中的子组件调用父函数

python - 如何用python中的函数改变列表?

c - 如何存储系统函数中的字符串

c - 为什么 0XAA 是无符号整数而不是整数?

scala - 案例类中的 def vs lazy val

c# - Windows Phone 8 - 枚举和数据注释

c - 减号运算符用作一元按位运算

c - 无法定位 Bug