修复C中的错误时出错

时间:2016-07-24 17:16:55

标签: c

我有一个.c文件和两个标头。我不知道为什么会出现这些错误。我试图在网上找到解决方案,但无法将其应用到我的代码中。

Structs.h

#ifndef STRUCTS_H
#define STRUCTS_H

typedef struct stream
{
    char* enc; //binary string
    int pos;
}stream;

typedef struct tag
{
    int tagClass;
    char tagConstructed;
    char tagNumber;
}tag;

#endif

Funcs.h错误:

Error   22  error C2143: syntax error : missing '{' before '&'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    15  1   asn1
Error   26  error C2143: syntax error : missing '{' before '&'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    36  1   asn1
Error   21  error C2143: syntax error : missing ')' before '&'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    15  1   asn1
Error   25  error C2143: syntax error : missing ')' before '&'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    36  1   asn1
Error   15  error C2061: syntax error : identifier 'isUniversal'    c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    3   1   asn1
Error   18  error C2061: syntax error : identifier 'isEOC'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    7   1   asn1
Error   17  error C2059: syntax error : 'type'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    3   1   asn1
Error   20  error C2059: syntax error : 'type'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    7   1   asn1
Error   16  error C2059: syntax error : ';' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    3   1   asn1
Error   19  error C2059: syntax error : ';' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    7   1   asn1
Error   24  error C2059: syntax error : ')' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    15  1   asn1
Error   28  error C2059: syntax error : ')' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    36  1   asn1
Error   23  error C2059: syntax error : '&' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    15  1   asn1
Error   27  error C2059: syntax error : '&' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    36  1   asn1


#ifndef FUNCS_H
#define FUNCS_H

#include "Structs.h"

bool isUniversal(tag curTag);
bool isEOC(tag curTag);
char getByte(stream curStream);
void parseTag(stream & str, tag & tag);
char parseLength(stream & str);

#endif

Functions.c错误:

Error   22  error C2143: syntax error : missing '{' before '&'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    15  1   asn1
Error   26  error C2143: syntax error : missing '{' before '&'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    36  1   asn1
Error   21  error C2143: syntax error : missing ')' before '&'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    15  1   asn1
Error   25  error C2143: syntax error : missing ')' before '&'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    36  1   asn1
Error   15  error C2061: syntax error : identifier 'isUniversal'    c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    3   1   asn1
Error   18  error C2061: syntax error : identifier 'isEOC'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    7   1   asn1
Error   17  error C2059: syntax error : 'type'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    3   1   asn1
Error   20  error C2059: syntax error : 'type'  c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    7   1   asn1
Error   16  error C2059: syntax error : ';' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    3   1   asn1
Error   19  error C2059: syntax error : ';' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    7   1   asn1
Error   24  error C2059: syntax error : ')' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    15  1   asn1
Error   28  error C2059: syntax error : ')' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    36  1   asn1
Error   23  error C2059: syntax error : '&' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    15  1   asn1
Error   27  error C2059: syntax error : '&' c:\users\natasha\documents\visual studio 2012\projects\asn1\asn1\functions.c    36  1   asn1


#include "Funcs.h"

bool isUniversal(tag curTag) {
    return curTag.tagClass == 0x00;
}

bool isEOC(tag curTag) {
    return (curTag.tagClass == 0x00) && (curTag.tagNumber == 0x00);
}

char getByte(stream curStream) {
    return curStream.enc[curStream.pos];
}

void parseTag(stream & str, tag & tag)
{
    char buf, tempNumber;
    buf = getByte(str);
    tempNumber = 0x00;
    tag.tagClass = buf >> 6;
    tag.tagConstructed = ((buf & 0x20) != 0);
    tag.tagNumber = buf & 0x1F;
    if (tag.tagNumber == 0x1F)
    {
        do
        {
            str.pos++;
            buf = getByte(str);
            tempNumber = (tempNumber << 7) | (buf & 0x7F);
        }while(buf & 0x80);

        tag.tagNumber = tempNumber;
    }
}

char parseLength(stream & str) {
    char buf, len;
    int numberOfOctets, i;
    buf = getByte(str);
    len = 0x00;
    numberOfOctets = 0;
    if (buf < 0x80) {
        len = buf; //short form
    } else { //long form
        numberOfOctets = buf & 0x7F;
        for (i = 0; i < numberOfOctets; ++i) {
            len = (len * 256) + getByte(str);
            str.pos++;
        }
    }

    return len;
}

1 个答案:

答案 0 :(得分:1)

C没有像C ++那样的引用。由于void parseLength(stream & str);&的语法无效。不过,你可以改用指针。

例如,要将parseLength()函数转换为使用指针(并更改返回类型以匹配定义),原型将变为:

char parseLength(stream *str);

在函数体中,您需要根据需要取消引用指针,如下所示:

char parseLength(stream *str) {
    char buf, len;
    int numberOfOctets, i;
    buf = getByte(*str); /* use "*" to get the object from the pointer */
    len = 0x00;
    numberOfOctets = 0;
    if (buf < 0x80) {
        len = buf; //short form
    } else { //long form
        numberOfOctets = buf & 0x7F;
        for (i = 0; i < numberOfOctets; ++i) {
            len = (len * 256) + getByte(*str);
            str->pos++; /* Use "->" to access struct members from the pointer */
        }
    }

    return len;
}

此外,您需要传递地址(指针)而不是实际对象,因此您还需要更改对函数的调用,例如。 parseLength(str)parseLength(&str)

您当然需要对parseTag()以及尝试使用引用的任何其他代码进行类似的更改。

可能像getByte()这样的其他一些函数也应该使用指针而不是实际的结构...例如,如果getByte()需要修改pos成员它被传递的stream参数使得它影响来自调用者的对象,而不仅仅是它的副本。

另外,parseLength()可能应该返回intsize_t而不是char - len看起来需要比{{1}更大的范围{1}}。