简体   繁体   English

结构函数指针错误

[英]Struct function pointer error

In the struct my_struct, there is a function pointer called compute(). 在结构my_struct中,有一个函数指针称为compute()。 It is declared as such: 声明如下:

struct my_struct
{
  double (*compute) (double input);
}

In a separate file, I initialize that struct so I can point that function to another one. 在一个单独的文件中,我初始化了该结构,因此可以将该函数指向另一个函数。

static const struct my_struct data;
data.compute = ......

The problem is, no matter what I set the function pointer to, I get the following error for data.compute: 问题是,无论我将函数指针设置为什么,都会对data.compute产生以下错误:

error: expected '=', ',', ';', 'asm', or '__attribute__' before '.' token

I've used data members of structs plenty of times using the '.' 我已经多次使用'。'来使用结构的数据成员。 operator, but I've never used function pointers. 运算符,但我从未使用过函数指针。 Is there something different needed here? 这里需要其他东西吗?

It should work notationally, though since you've defined the structure as const , you can only initialize it and not assign to it after initialization. 尽管您已经将结构定义为const ,但是它应该可以在符号上起作用,但是您只能对其进行初始化,而不能在初始化后分配给它。

However, that's a different error from the one you're getting. 但是,这是与您得到的错误不同的错误。 It's behaving a bit as if data isn't a simple word — as if it is macro expanded into something odd, or something along those lines. 它表现得好像data不是一个简单的单词,就像它在宏中扩展为奇数或沿这些线的东西一样。 The structure type is declared in a header, isn't it? 结构类型是在标头中声明的,不是吗? And it does have a semicolon after the }, doesn't it? 并且在}之后确实有分号,不是吗?

Yeah, const isn't the problem. 是的, const不是问题。 I've tried removing it, only to get the same error. 我试着删除它,只是得到相同的错误。 Any idea how to solve that last part you're talking about? 知道如何解决您正在谈论的最后一部分吗?

At one level, there's not enough code — you've not provided an MCVE ( Minimal, Complete, and Verifiable Example ) — we have no code that we can compile and see the error you're seeing (or something similar). 一方面,没有足够的代码-您没有提供MCVE( 最小,完整和可验证的示例 )-我们没有可编译的代码,也看不到您看到的错误(或类似错误)。 We'd need your header and a minimal set of code that shows the problem. 我们需要您的标头和少量显示该问题的代码。

You are writing the data.compute = … inside a function, aren't you? 您正在将data.compute = …写入函数内部,不是吗? ( Hmmm: I suspect not — you must either use initialization … data = { … }; or move the assignment inside a function.) 嗯:我怀疑不是 -您必须使用初始化… data = { … };或将赋值移动到函数内部。)

No, it's not in a function. 不,它不在函数中。 Could you elaborate a little more on … data = { … }; 您能否详细说明… data = { … }; ? I don't recognize that syntax; 我不了解这种语法; what does the first represent? 第一个代表什么?

The first is static const struct my_struct but I was feeling too lazy to copy'n'paste. 第一个static const struct my_struct但是我觉得太懒了以至于无法复制粘贴。 So, you need: 因此,您需要:

static const struct my_struct data = { .compute = sin };

or something similar (assuming you include the <math.h> to provide a declaration for sin — or use some other function that you've already declared or defined). 或类似的内容(假设您包括<math.h>来提供sin的声明-或使用已经声明或定义的其他函数)。 If you are stuck without a C99 or later compiler): 如果您没有使用C99或更高版本的编译器而陷入困境):

static const struct my_struct data = { sin };

You can't write assignments outside of functions — that is your problem. 您不能在函数之外编写分配,这就是您的问题。 You must use an initializer, or write the assignment inside a function and remove the const . 您必须使用初始化程序,或在函数内部编写赋值并删除const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM