请求成员“a”,而不是结构或工会。

时间:2022-01-14 21:21:22

I looked through the previous posts on the topic to no avail. I'm attempting to access an array within a struct in an incorrect way and am getting the error request for member ‘Days’ in something not a structure or union.

我浏览了前面的帖子,但没有成功。我试图以不正确的方式访问struct中的数组,并在某个非结构或联合的东西中得到成员“Days”的错误请求。

The relevant line in my .c:

在我的。c:

    bool isConflict(TimeSpan *timeA, TimeSpan *timeB, Class *classA[], Class *classB[])
    {       // Checking that days themselves conflict
        int i;
        for (i = 0; i < 7; i++) {
                if ((classA->Days[i] == classB->Days[i]) && (classA->Days[i] != 0)){
                         doStuff;
    }

The error itself is happening at this bottom line in the way I'm attempting to access Days[]. Days itself is prototyped in my .h as such with prototypes :

在我尝试访问天数的方式中,错误本身发生在这个底线。在我的h中,天本身就是原型:

    // TimeSpan is a previously created struct that is functioning correctly
    typedef struct Class
    {       TimeSpan timeSpan;
            int Days[7];
    } Class;

    bool isConflict(TimeSpan * a, TimeSpan * b, Class * classA[], Class * classB[]);

And lastly, my driver call looks like:

最后,我的司机电话是这样的:

    if(isConflict(&EE315.timeSpan, &EE367.timeSpan, &EE315.Days[7], &EE367.Days[7])) 

Thanks in advance for any help! - Allen

感谢您的帮助!-艾伦


EDIT: Thanks to everyone including Floris and ooga for helping me out. Both of your tips were spot on and I appreciate it. - Allen

感谢所有人,包括Floris和ooga帮我走出了难关。你的两个建议都很好,我很感激。-艾伦

2 个解决方案

#1


2  

You are calling with

你打电话

if(isConflict(&EE315.timeSpan, &EE367.timeSpan, &EE315.Days[7], &EE367.Days[7])) 

So your third argument is

第三个论点是。

&EE315.Days[7]

which is a pointer to element 7 of the array Days. What you want is

它是数组中元素7的指针。你想要的是

&EE315

That is a pointer to your structure... So the call becomes

这是一个指向你的结构的指针…所以电话变成了

if(isConflict(&EE315.timeSpan, &EE367.timeSpan, &EE315, &EE367)) 

See if that works for you.

看看这对你是否有效。

Note also that you are declaring your function as

还要注意,您正在声明您的函数。

bool isConflict(TimeSpan * a, TimeSpan * b, Class * classA[], Class * classB[]);

You probably want to use

你可能想用。

bool isConflict(TimeSpan * a, TimeSpan * b, Class * classA, Class * classB);

Since you are pointing to an element, not an array of elements (I think).

因为您指向的是一个元素,而不是一个元素数组(我认为)。

afterthought

事后的想法

It might be easier to change your code as follows:

更改代码可能更容易:

bool isConflict( Class *a, Class *b ) {
  int i;
  for (i = 0; i < 7; i++) {
    if ((a->Days[i] == b->Days[i]) && (a->Days[i] != 0)){
      doStuff;
    }
}

Since both timeSpan and Days[] are in fact part of the same structure. From a code readability perspective it's much better. And then you call it with

因为timeSpan和Days都是相同结构的一部分。从代码可读性的角度来看,它要好得多。然后你把它叫做。

if(isConflict(&EE315, &EE367)) 

See how much neater that is?

看看这有多整洁?

#2


1  

I think you mean to have this prototype:

我想你是想要这个原型:

bool isConflict(TimeSpan * a, TimeSpan * b, int daysA[], int daysB[]);

and call it like this:

这样称呼它:

if(isConflict(&EE315.timeSpan, &EE367.timeSpan, &EE315.Days, &EE367.Days)) 

Obviously you'll have to change the definition of the function to access the new parameter type.

显然,您必须更改函数的定义以访问新的参数类型。

#1


2  

You are calling with

你打电话

if(isConflict(&EE315.timeSpan, &EE367.timeSpan, &EE315.Days[7], &EE367.Days[7])) 

So your third argument is

第三个论点是。

&EE315.Days[7]

which is a pointer to element 7 of the array Days. What you want is

它是数组中元素7的指针。你想要的是

&EE315

That is a pointer to your structure... So the call becomes

这是一个指向你的结构的指针…所以电话变成了

if(isConflict(&EE315.timeSpan, &EE367.timeSpan, &EE315, &EE367)) 

See if that works for you.

看看这对你是否有效。

Note also that you are declaring your function as

还要注意,您正在声明您的函数。

bool isConflict(TimeSpan * a, TimeSpan * b, Class * classA[], Class * classB[]);

You probably want to use

你可能想用。

bool isConflict(TimeSpan * a, TimeSpan * b, Class * classA, Class * classB);

Since you are pointing to an element, not an array of elements (I think).

因为您指向的是一个元素,而不是一个元素数组(我认为)。

afterthought

事后的想法

It might be easier to change your code as follows:

更改代码可能更容易:

bool isConflict( Class *a, Class *b ) {
  int i;
  for (i = 0; i < 7; i++) {
    if ((a->Days[i] == b->Days[i]) && (a->Days[i] != 0)){
      doStuff;
    }
}

Since both timeSpan and Days[] are in fact part of the same structure. From a code readability perspective it's much better. And then you call it with

因为timeSpan和Days都是相同结构的一部分。从代码可读性的角度来看,它要好得多。然后你把它叫做。

if(isConflict(&EE315, &EE367)) 

See how much neater that is?

看看这有多整洁?

#2


1  

I think you mean to have this prototype:

我想你是想要这个原型:

bool isConflict(TimeSpan * a, TimeSpan * b, int daysA[], int daysB[]);

and call it like this:

这样称呼它:

if(isConflict(&EE315.timeSpan, &EE367.timeSpan, &EE315.Days, &EE367.Days)) 

Obviously you'll have to change the definition of the function to access the new parameter type.

显然,您必须更改函数的定义以访问新的参数类型。