gpt4 book ai didi

c - 修复 C 中的错误时出现的错误

转载 作者:行者123 更新时间:2023-11-30 20:22:46 25 4
gpt4 key购买 nike

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

结构体.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;
}

最佳答案

C 不像 C++ 那样有引用。由于 &void parseLength(stream & str); 不是有效语法。不过,您可以使用指针来代替。

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

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() 需要修改所传递的 stream 参数的 pos 成员,以便它影响来自调用者的对象,而不仅仅是它的副本。

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

关于c - 修复 C 中的错误时出现的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38554666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com