cvisual-studio-2010structureheader-files

Structure type not identified?


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.


Solution

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