编译错误:没有这样的文件或目录
问题描述:
当我尝试编译我的代码,我得到这个:编译错误:没有这样的文件或目录
fatal error: includes.h: No such file or directory.
为什么抛出这个错误?
这个程序应该展示链表。
#include "includes.h"
ListType list[5];
int main()
{
ListType list[5];
list[0].value = 0; list[0].next = list+1;
list[1].value = 10; list[1].next = list+2;
list[2].value = 20; list[2].next = NULL;
ListReport(list);
// Insert an element
list[3].value = 15; list[3].next = list+2;
list[1].next = list+3;
ListReport(list);
// Remove an element
list[0].next = list+3;
ListReport(list);
return 0;
}
// Report values in linked lisk
void ListReport(ListType *plist)
{
int nn = 0;
printf("ListReport\n");
while(plist != NULL){
printf("%d, value = %d\n",nn++, plist->value);
plist = plist->next;
}
}
/*******************************************************
* includes.h
******************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct entry
{
int value;
struct entry *next;
} ListType;
void ListReport(ListType *plist);
// end of includes.h
答
您的代码似乎将您的.cpp和.h文件连接成一个。你需要将它们分成两个文件。
像这样:
的main.cpp
#include "includes.h"
ListType list[5];
int main()
{
ListType list[5];
list[0].value = 0; list[0].next = list+1;
list[1].value = 10; list[1].next = list+2;
list[2].value = 20; list[2].next = NULL;
ListReport(list);
// Insert an element
list[3].value = 15; list[3].next = list+2;
list[1].next = list+3;
ListReport(list);
// Remove an element
list[0].next = list+3;
ListReport(list);
return 0;
}
// Report values in linked lisk
void ListReport(ListType *plist)
{
int nn = 0;
printf("ListReport\n");
while(plist != NULL){
printf("%d, value = %d\n",nn++, plist->value);
plist = plist->next;
}
}
INCLUDES.H
#include <stdio.h>
#include <stdlib.h>
typedef struct entry
{
int value;
struct entry *next;
} ListType;
void ListReport(ListType *plist);
或者像你这样,你可以合并文件和从顶部除去#include语句。
+0
谢谢你是对的 – johnsmith
+0
尝试在使用它之前将typedef语句放在顶部 –
includes.h与主文件位于同一个文件夹中吗? – vadim