开发者

Structure type not identified?

开发者 https://www.devze.com 2023-02-20 21:37 出处:网络
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: lab

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 lab开发者_StackOverflowel 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.


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 *).)

0

精彩评论

暂无评论...
验证码 换一张
取 消