未识别结构类型?
我有一个指向具体数据类型结构的抽象数据类型。功能位于 C 文件中,而定义位于头文件中。编译器给我以下错误: labelgen.c(20): error C2065: 'labelGeneratorCDT' : undeclared identifier
Here's header:
#ifndef _labelgen_h
#define _labelgen_h
// labelGeneratorCDT is defined as a structure
// for a label generator, with labelGeneratorADT
// being a pointer to it
typedef struct labelGeneratorCDT* labelGeneratorADT;
// Function to generate a new labelGeneratorADT
labelGeneratorADT NewLabelGenerator(char* prefix, int sequenceNum);
// Function to retrieve the new label string
char* GetNextLabel(labelGeneratorADT labelGenerator);
// Function to free the memory allocated to the labelGeneratorADT
void FreeLabelGenerator(labelGeneratorADT labGenerator);
#endif
Here's the C file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "labelgen.h"
// labelGenerator CDT has a prefix string
// and a sequence number to start with
struct labelGeneratorCDT
{
char* prefix;
int sequenceNum;
};
labelGeneratorADT NewLabelGenerator(char* prefix, int sequenceNum)
{
// Start with NULL, shorter line
labelGeneratorADT labelGenerator = NULL;
// Allocate enough memory to fit a labelGeneratorCDT
labelGenerator = (labelGeneratorCDT*)(malloc(sizeof(labelGeneratorCDT)));
// Allocate memory for the prefix string
labelGenerator->prefix = (char*)(malloc((strlen(prefix)+1)*(sizeof(char))));
// Copy over prefix
strcpy(labelGenerator->prefix,prefix);
// Simply copy the sequence number
labelGenerator->sequenceNum = sequenceNum;
return labelGenerator;
}
char* GetNextLabel(labelGeneratorADT labelGenerator)
{
return "0";
}
void FreeLabelGenerator(labelGeneratorADT labGenerator)
{
return;
}
代码显然非常简单,但我是 C 结构的初学者,我一生都无法弄清楚为什么这不能编译。这是在 Visual Studio 2010 中完成的。
I have an abstract data type that points to a concrete data type structure. The functionality is in a C file, while the definition is in the header file. The compiler gives me the following error: labelgen.c(20): error C2065: 'labelGeneratorCDT' : undeclared identifier
Here's the header:
#ifndef _labelgen_h
#define _labelgen_h
// labelGeneratorCDT is defined as a structure
// for a label generator, with labelGeneratorADT
// being a pointer to it
typedef struct labelGeneratorCDT* labelGeneratorADT;
// Function to generate a new labelGeneratorADT
labelGeneratorADT NewLabelGenerator(char* prefix, int sequenceNum);
// Function to retrieve the new label string
char* GetNextLabel(labelGeneratorADT labelGenerator);
// Function to free the memory allocated to the labelGeneratorADT
void FreeLabelGenerator(labelGeneratorADT labGenerator);
#endif
Here's the C file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "labelgen.h"
// labelGenerator CDT has a prefix string
// and a sequence number to start with
struct labelGeneratorCDT
{
char* prefix;
int sequenceNum;
};
labelGeneratorADT NewLabelGenerator(char* prefix, int sequenceNum)
{
// Start with NULL, shorter line
labelGeneratorADT labelGenerator = NULL;
// Allocate enough memory to fit a labelGeneratorCDT
labelGenerator = (labelGeneratorCDT*)(malloc(sizeof(labelGeneratorCDT)));
// Allocate memory for the prefix string
labelGenerator->prefix = (char*)(malloc((strlen(prefix)+1)*(sizeof(char))));
// Copy over prefix
strcpy(labelGenerator->prefix,prefix);
// Simply copy the sequence number
labelGenerator->sequenceNum = sequenceNum;
return labelGenerator;
}
char* GetNextLabel(labelGeneratorADT labelGenerator)
{
return "0";
}
void FreeLabelGenerator(labelGeneratorADT labGenerator)
{
return;
}
The code is obviously very simple, but I'm a beginner with C structures, and for the life of me I can't figure out why this won't compile. This was done in visual studio 2010.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C++将为
struct labelGeneratorCDT
定义一个类型labelGeneratorCDT
; C 没有。 (请注意.h
文件如何对labelGeneratorADT
使用typedef
。在 C++ 中,这会导致问题,因为labelGeneratorADT
会已存在,无法将 typedef 编辑为 (struct labelGeneratorADT *)。)C++ will define a type
labelGeneratorCDT
forstruct labelGeneratorCDT
; C does not. (Notice how the.h
file uses atypedef
forlabelGeneratorADT
. In C++ that would cause problems becauselabelGeneratorADT
would already exist and could not betypedef
ed to(struct labelGeneratorADT *)
.)