Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to do this function but i have the error
pointer to incomplete class type is not allowed
when i use variable of Map !!!

What I have tried:

C++
typedef struct Map_t* Map;

struct map_t
{
    char key;
    char value;
    int iterator;
    struct map_t* next;
};

Map mapCreate()
{
    Map new_map = (Map)malloc(sizeof(struct map_t));
    if (new_map == NULL)
    {
        return  NULL;
    }

    new_map->key = NULL;
    new_map->value = NULL;
    new_map->next = NULL;
    new_map->iterator = 0;
    return new_map;
}
Posted
Updated 25-Jun-22 13:08pm
v3

You using struct map_t as a member of the struct. This requires the "forward declaration"

C++
/*Forward declaration*/
struct map_t;

struct map_t
{
    char key;
    char value;
    int iterator;
    struct map_t* next;/*at this point this is still an incomplete type thus the forward declaration above*/
};
 
Share this answer
 
v2
Comments
Rick York 14-Apr-20 4:17am    
No, a forward declaration is not required. The code will compile without it.

-edit- that is, VS2017 will compile it.
steveb 14-Apr-20 13:42pm    
Yes, you right my bad. I think some older compilers required forward decl
The reason for the error is obviously that map_t capitalized does not correspond to any declaration.

C
// typedef struct Map_t* Map;
typedef struct map_t* Map;


Additionally warnings are generated, because NULL corresponds to a null pointer, but key and value are defined as char.

I also wonder why the source code was tagged as C++, although pure C is used here.
With C++ I would suggest a class instead of the structure and the function and new() instead of the function malloc().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900