函数参数中的未知类型名称
Unknown type name in function parameters
我有这个 header 文件。
// Bunch of constants defined first
typedef char Token[MAX_TOKEN + 1];
typedef struct
{
Token szToken;
int iCategory;
int iSubcategory;
int iPrecedence;
} Element;
typedef struct
{
int iCount;
Element stackElementM[MAX_STACK_ELEM];
} StackImp;
typedef StackImp *Stack;
typedef struct
{
int iOutCount;
Element outM[MAX_OUT_ITEM];
} OutImp;
typedef OutImp *Out;
// Stack functions
void push(Stack stack, Element value);
Element pop(Stack stack);
int isEmpty(Stack stack);
Stack newStack();
void freeStack(Stack stack);
Element topElement(Stack stack);
void categorize(Element *pElement);
Out newOut();
void addOut(Out out, Element element);
void printOut(Out out);
int convertToPostfix(char *pszInfix, Out out); // This is confusing me...
void ErrExit(int iexitRC, char szFmt[], ...);
char * getToken(char *pszInputTxt, char szToken[], int iTokenSize);
此 header 由调用 int convertToPostfix(char *pszInfix, Out out)
的 driver 使用,问题是我的编译器中一直出现 unknown type name 'Out'
作为错误。 convertToPostfix 函数应该使用一个标记来指示是否有任何错误,并通过 addOut 函数(在 driver 中)填充 out 数组,但我无能为力,因为这个错误阻止了我编译! convertToPostfix()
函数不是 driver 的一部分,如果这很重要,它位于单独的 C 文件中。
任何帮助都会很棒!
编辑:我首先为 driver 生成一个 .o 文件,然后我为 convertToPostfix() 的 .c 文件做同样的事情,此时
编译器
gcc -g -c -o myDriver.o myDriver.c //Complies fine.
gcc -g -c -o pretoPost.o pretoPost.c
pretoPost.c:7:38: error: unknown type name ‘Out’
int convertToPostfix(char *pszInfix, Out out)
我得到了错误。
编辑 2:非常感谢人们的帮助!错误是我没有将 #include "header.h"
包含到我的 pretoPost.c 文件中!
正在将评论转移到答案。
如果 Garr Godfrey 提供了答案,请采纳——他提供了问题的第一个指针。
问题的第一个指针
Garr Godfrey [评论]):
"Separate C file" — does that mean it is being compiled, too? Is the header actually being included by the file the error shows up in, because it should be.
指向问题的扩展评论
Does pretoPost.c
have #include "mystery.h"
in it (where "mystery.h
" is your unnamed header file)? If not, that's the trouble.
:
Note that the error message is about pretoPost.c
not understanding Out
, not about the header not understanding Out
.
这似乎就是问题所在!
Also note that you've not defined a prototype for newStack()
or newOut()
in particular. You've declared that the functions exist, but you've not specified the parameter list, so any number and any types of values are allowed. You need Out newOut(void);
and Stack newStack(void);
to declare prototypes for the functions. This is C, not C++ — in C++, the rules would be different. (This may just be sloppy coding by your professor if he supplied the header.)
这是一个常见的误解。函数声明如:
void *somefunction();
简单地说 somefunction
存在并且 returns 一个 void *
值。它没有直接指定参数的类型或数量,但函数不能是可变的(如 printf()
,采用可变数量的参数),因为你必须 always 在使用可变参数函数之前在范围内有一个原型。要说 'no arguments',C 要求:
void *somefunction(void);
现在你有了一个原型。如果使用 GCC 编译,请考虑:
gcc -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes …
需要一点时间来适应这种强加的纪律,但它可以确保您跳过测试很多错误。
我有这个 header 文件。
// Bunch of constants defined first
typedef char Token[MAX_TOKEN + 1];
typedef struct
{
Token szToken;
int iCategory;
int iSubcategory;
int iPrecedence;
} Element;
typedef struct
{
int iCount;
Element stackElementM[MAX_STACK_ELEM];
} StackImp;
typedef StackImp *Stack;
typedef struct
{
int iOutCount;
Element outM[MAX_OUT_ITEM];
} OutImp;
typedef OutImp *Out;
// Stack functions
void push(Stack stack, Element value);
Element pop(Stack stack);
int isEmpty(Stack stack);
Stack newStack();
void freeStack(Stack stack);
Element topElement(Stack stack);
void categorize(Element *pElement);
Out newOut();
void addOut(Out out, Element element);
void printOut(Out out);
int convertToPostfix(char *pszInfix, Out out); // This is confusing me...
void ErrExit(int iexitRC, char szFmt[], ...);
char * getToken(char *pszInputTxt, char szToken[], int iTokenSize);
此 header 由调用 int convertToPostfix(char *pszInfix, Out out)
的 driver 使用,问题是我的编译器中一直出现 unknown type name 'Out'
作为错误。 convertToPostfix 函数应该使用一个标记来指示是否有任何错误,并通过 addOut 函数(在 driver 中)填充 out 数组,但我无能为力,因为这个错误阻止了我编译! convertToPostfix()
函数不是 driver 的一部分,如果这很重要,它位于单独的 C 文件中。
任何帮助都会很棒!
编辑:我首先为 driver 生成一个 .o 文件,然后我为 convertToPostfix() 的 .c 文件做同样的事情,此时
编译器
gcc -g -c -o myDriver.o myDriver.c //Complies fine.
gcc -g -c -o pretoPost.o pretoPost.c
pretoPost.c:7:38: error: unknown type name ‘Out’
int convertToPostfix(char *pszInfix, Out out)
我得到了错误。
编辑 2:非常感谢人们的帮助!错误是我没有将 #include "header.h"
包含到我的 pretoPost.c 文件中!
正在将评论转移到答案。
如果 Garr Godfrey 提供了答案,请采纳——他提供了问题的第一个指针。
问题的第一个指针
Garr Godfrey [评论]):
"Separate C file" — does that mean it is being compiled, too? Is the header actually being included by the file the error shows up in, because it should be.
指向问题的扩展评论
Does
pretoPost.c
have#include "mystery.h"
in it (where "mystery.h
" is your unnamed header file)? If not, that's the trouble.
Note that the error message is about
pretoPost.c
not understandingOut
, not about the header not understandingOut
.
这似乎就是问题所在!
Also note that you've not defined a prototype for
newStack()
ornewOut()
in particular. You've declared that the functions exist, but you've not specified the parameter list, so any number and any types of values are allowed. You needOut newOut(void);
andStack newStack(void);
to declare prototypes for the functions. This is C, not C++ — in C++, the rules would be different. (This may just be sloppy coding by your professor if he supplied the header.)
这是一个常见的误解。函数声明如:
void *somefunction();
简单地说 somefunction
存在并且 returns 一个 void *
值。它没有直接指定参数的类型或数量,但函数不能是可变的(如 printf()
,采用可变数量的参数),因为你必须 always 在使用可变参数函数之前在范围内有一个原型。要说 'no arguments',C 要求:
void *somefunction(void);
现在你有了一个原型。如果使用 GCC 编译,请考虑:
gcc -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes …
需要一点时间来适应这种强加的纪律,但它可以确保您跳过测试很多错误。