c - ‘;’ token 之前出现错误 : expected ‘,’ , ‘)’ 或 ‘.’

标签 c data-structures struct token

我很长一段时间以来第一次使用struct,并且遇到了多个问题。我只是想系统地解决这些问题,但这个问题尤其让我感到困难。当我尝试make程序时,我得到了这个:

`datime.c:9:23:error: expected ‘;’, ‘,’ or ‘)’ before ‘.’ token bool 
isConflict(Class.Time*, Class.Time*){` 

`datime.c:18:21: error: expected ‘;’, ‘,’ or ‘)’ before ‘.’ token bool 
isEarlier(Time.Start*, Time.Start*){`.

我的文件,schedule.h:

/*
 *      file: datime.h
 */

typedef struct
{       int hour;
        int min;
} Start;

typedef struct
{       int hour;
        int min;
} Stop;

typedef struct
{       struct Start;
        struct Stop;
} Time;

typedef struct
{       struct Time;
        int Days[7];
} Class;

bool isConflict(struct TimeA*, struct TimeB*);

bool isEarlier(struct TimeA*, struct TimeB*);

我的文件,schedule.c:

/*
 *      file: datime.c
 */

#include <stdio.h>
#include <stdbool.h>
#include "datime.h"

bool isConflict(Class.Time*, Class.Time*){
        free(classA);
        free(classB);
        if ((classA.Start <= classB.Start) && (classA.Stop >= classB.Stop))
                return 1;
        else
                return 0;
}

bool isEarlier(Time.Start*, Time.Start*){
        free(classA);
        free(classB);
        if (classA.Start < classB.Start)
                return 1;
        else
                return 0;
}

此外,driver.c,仅用于测试函数和struct:

#include <stdbool.h>
#include "datime.h"

main()
{
        void setClass(struct Class)
        {       struct Class EE205{
                        EE205.Time.Start.hour = 8;
                        EE205.Time.Start.min = 30;
                        EE205.Time.Stop.hour = 9;
                        EE205.Time.Stop.min = 20;

                        // Initializing array to zero
                        memset(EE205.Days, 0, 7 * sizeof(Days[0]));
                        EE205.Days[1] = 1;
                        EE205.Days[3] = 1;
                        EE205.Days[5] = 1;
                };

                struct Class EE367{
                        EE367.Time.Start.hour = 10;
                        EE367.Time.Start.min = 30;
                        EE367.Time.Stop.hour = 11;
                        EE367.Time.Stop.min = 20;

                        // Initializing array to zero
                        memset(EE367.Days, 0, 7 * sizeof(Days[0]));
                        EE367.Days[1] = 1;
                        EE367.Days[3] = 1;
                        EE367.Days[5] = 1;
                };

                struct Class EE315{
                        EE315.Time.Star.hour = 12;
                        EE315.Time.Start.min = 30;
                        EE315.Time.Stop.hour = 13;
                        EE315.Time.Stop.min = 20;

                        // Initializing array to zero
                        memset(EE315.Days, 0, 7 * sizeof(Days[0]));
                        EE315.Days[1] = 1;
                        EE315.Days[3] = 1;
                        EE315.Days[5] = 1;
                };
        if(isConflict(EE315, EE367))
                printf("Scheduling conflict! EE315 and EE367 conflict.");
        if(isConflict(EE315, EE205))
                printf("Scheduling conflict! EE315 and EE205 conflict.");
        if(isConflict(EE205, EE367))
                printf("Scheduling conflict! EE205 and EE367 conflict.");

}

有很多错误,但这些错误是我出于某种原因无法移动的两个错误。提前致谢。

  • 艾伦

最佳答案

这不是任何合法的 C 语法。

 bool isConflict(Class.Time*, Class.Time*)

(实际上几乎没有任何代码是有效的。我建议您从一个更小的示例/程序开始学习结构体和指针)

您必须将其声明为例如

bool isConflict(Class* classA, Class *classB){

body 也很奇怪,我实在搞不懂你想做什么:

        free(classA); <--- free() classA/classB, but 2 lines down you try to access them ?
        free(classB);

       // if ((classA.Start <= classB.Start) && (classA.Stop >= classB.Stop))
        //since classA/classB are pointers you need -> to access their members:
        // You also need to compare the members of Time, you can't compare structs
        // in C, e.g. to check the hours:
         if ((classA->Start.hour <= classB->Start.hour) && 
             (classA->Stop.hour >= classB->Stop.hour))
                return 1;
        else
                return 0;
}

您的 isEearlier 函数也有同样的问题。

main()
{
       //you can't declare a function within another function like this.
       //what is the purpose of the setClass function ? There is no code that calls this function
       //you also need to give names to your function parameters, if you want a pointer, it'd be
       //void setClass(struct Class *my_class)

        void setClass(struct Class)
        { 

您的Time结构是错误的,您没有为成员提供任何变量名称,它应该是:

typedef struct
{       Start Start;
        Stop Stop;
} Time;

(但是,您的 struct Startstruct Stop 是相同的,无需为两者创建 2 个结构体定义)

您的结构的初始值设定项是错误的:

struct Class EE205{
    EE205.Time.Start.hour = 8;
    EE205.Time.Start.min = 30;
    EE205.Time.Stop.hour = 9;
    EE205.Time.Stop.min = 20;
}; 

请注意,您说的是struct Class,但您没有在任何地方定义struct Class。你有一个 typedef 对于结构体,typedef 被称为 Class,因此编译器不会知道 struct Class,它只知道 Class

您可以设置成员,例如:

Class EE205;
EE205.Time.Start.hour = 8;
EE205.Time.Start.min = 30;
EE205.Time.Stop.hour = 9;
EE205.Time.Stop.min = 20;
// Initializing array to zero
memset(EE205.Days, 0, 7);
EE205.Days[1] = 1;
EE205.Days[3] = 1;
EE205.Days[5] = 1;

或者初始化它:

Class EE205 = {
  {
    {8, 30},
    {9,20}
  },
  {0,1,0,1,0,1}
};

关于c - ‘;’ token 之前出现错误 : expected ‘,’ , ‘)’ 或 ‘.’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253845/

相关文章:

c - mulcross数据生成器运行错误: free():invalid next size

c - Microsoft Visual C++ Express 中的 Lotus C API 示例

c - 在 C 中使用标记串联访问和修改变量

python - Python中最有效的图数据结构是什么?

c++ - vector<struct> 上的段错误

c++ - Makefile 总是重新编译一个文件

data-structures - 为什么RB树的根是黑色的?

algorithm - 二进制搜索 - 最好和最坏的情况

将可变长度结构复制到缓冲区

c - 在结构中初始化一个数组