未识别结构类型?

发布于 2024-10-28 09:06:01 字数 1883 浏览 2 评论 0原文

我有一个指向具体数据类型结构的抽象数据类型。功能位于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2024-11-04 09:06:01

C++将为struct labelGeneratorCDT定义一个类型labelGeneratorCDT; C 没有。 (请注意 .h 文件如何对 labelGeneratorADT 使用 typedef。在 C++ 中,这会导致问题,因为 labelGeneratorADT 会已存在,无法将 typedef 编辑为 (struct labelGeneratorADT *)。)

C++ will define a type labelGeneratorCDT for struct labelGeneratorCDT; C does not. (Notice how the .h file uses a typedef for labelGeneratorADT. In C++ that would cause problems because labelGeneratorADT would already exist and could not be typedefed to (struct labelGeneratorADT *).)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文