I have a problem in my code.
Here is my demo:
My file structure is
├── include
│ ├── link.h
│ └── token.h
└── src
├── CMakeLists.txt
└── main.c
//token.h
#ifndef __TOKEN_H__
#define __TOKEN_H__
#include <stdio.h>
#include "link.h"
typedef struct Token{
char val[30];
}token;
#endif
//link.h
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include <stdio.h>
#include "token.h"
typedef struct Linklist{
token* elem;
struct Linklist *next;
}linklist;
#endif
//main.c
#include <stdio.h>
#include "token.h"
#include "link.h"
#include <stdlib.h>
#include <string.h>
int main(){
linklist* head = (linklist*)malloc(sizeof(linklist));
head->elem = (token*)malloc(sizeof(token));
strcpy(head->elem->val, "111");
printf("%s\n", head->elem->val);
}
//CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(test VERSION 0.1.0)
include_directories(../include)
add_executable(test main.c)
Enter the src file and compile this demo
mkdir build && cd build
cmake ..
make
Then one error occurs:
error:
unknown type name 'token'
token* elem;
^
1 error generated.
But we don't use typedef, just use the struct Token, everything will be ok.
The modification version is:
//token.h
struct Token{
char val[30];
};
//link.h
typedef struct Linklist{
struct Token* elem;
struct Linklist *next;
}linklist;
//main.c
head->elem = (struct Token*)malloc(sizeof(struct Token));
I want to ask why does this situation happen?