错误:在'。'标记之前预期';',','或')'

时间:2014-01-21 09:21:15

标签: 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.");

}

有很多错误,但这些错误是我无法通过任何原因传递的两个错误。提前谢谢。

  • 阿伦

3 个答案:

答案 0 :(得分:4)

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

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

(实际上几乎没有代码是有效的。我建议你开始用更小的例子/程序学习结构和指针)

您必须将其声明为例如

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

身体也很奇怪,我无法弄清楚你打算做什么:

        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;
}

你的isEelier功能有同样的问题。

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}
};

答案 1 :(得分:0)

这是完全错误的,你不能使用结构类型作为结构成员。

/* 2 structures type doing the same thing? */
typedef struct
{       int hour;
        int min;
} Start;

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

typedef struct
{       struct Start; /* variable name missing */
        struct Stop;
} Time;

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

bool isConflict(struct TimeA*, struct TimeB*); /* makes no sense */

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

让我们改写它:

/* Replaced Start/Stop with time */
typedef struct
{       int hour;
        int min;
} Time;

/*
    Added member names.
    Removed struct because using typedef for Time
*/
typedef struct
{       Time start;
        Time stop;
} TimeSpan;

typedef struct
{       TimeSpan timeSpan;
        int Days[7];
} Class;

/*
    First type, then variable name for each parameter.
    Also removed the invalid struct marker.
*/
bool isConflict(Time * a, Time * b);

bool isEarlier(Time * a, Time * b);

这不能解决实际代码的问题,但现在你应该这样做了 至少让你的类型声明正确。

答案 2 :(得分:0)

您有多处错误。

  • 您正在使用typedef,因此无需在
  • 之后使用struct
  • 您只需在struct中输入类型即可忘记name
  • 在函数声明中相同

这是您的头文件:

/*
 *      file: datime.h
 */

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

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

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

typedef struct
{       Time time;
        int days[7];
} Class;

bool isConflict(Time *timeA, Time *timeB);

bool isEarlier(Time *timeA, Time *timeB);

在您的源文件中也存在错误:

  • 您正在使用struct指针,以便访问您必须使用->而不是.
  • 的成员
  • 您正在bool使用<stdbool.h>,因此您应该返回truefalse
  • 您不能free稍后您将访问的内容(或者您将遇到分段错误)

这是您的源文件:

/*
 *      file: datime.c
 */

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

bool isConflict(Time *timeA, Time *timeB) {
        if ((timeA->start <= timeB->start) && (timeA->stop >= timeB->stop))
                return true;
        else
                return false;
}

bool isEarlier(Time *timeA, Time *timeB) {
        if (timeA->start < timeB->start)
                return true;
        else
                return false;
}

你的主要还有很多错误。我让你看到其他的回应......