gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:48:50 25 4
gpt4 key购买 nike

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

测试设置.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/

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