当我尝试编译我的简单入门程序时,出现以下错误:
In file included from processcommand.c:1:
processcommand.h:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
processcommand.c:3: error: conflicting types for 'getinput'
processcommand.h:3: error: previous declaration of 'getinput' was here
processcommand.c: In function 'getinput':
processcommand.c:8: warning: return makes integer from pointer without a cast
processcommand.c: At top level:
processcommand.c:12: error: conflicting types for 'printoutput'
processcommand.h:4: error: previous declaration of 'printoutput' was here
我的代码文件是: 的 main.c中
#include "main.h"
int main() {
float version = 0.2;
printf("Qwesomeness Command Interprepter version %f by Zeb McCorkle starting...\n", version);
printf("%s", getcommand());
}
main.h
#include "includes.h"
int main();
INCLUDES.H
#include <stdio.h>
#include <string.h>
processcommand.c
#include "processcommand.h"
getinput() {
char *output;
printf(" ");
scanf("%s", output);
return output;
}
printoutput(char *input) {
printf("%s", input);
return 0;
}
getcommand() {
printoutput("Command:");
return getinput();
}
processcommand.h
#include "includes.h"
char *getinput();
unsigned char printoutput(char *input);
char *getcommand();
我相信这些都是我的源文件。我用
编译gcc main.c processcommand.c
感谢您阅读本文。
答案 0 :(得分:2)
在processcommand.c
中,它说
getinput()
什么是
的缩写int getinput()
这是一个带有未指定参数和int
返回值的函数。
但是,processcommand.h
中有
char *getinput()
这是一个带有未指定参数和char *
返回值的函数。
这两个地方你可能想要的是
char *getinput(void)
这是一个没有参数和char *
返回值的函数。
printoutput
和getcommand
有同样的问题。
答案 1 :(得分:0)
以下错误开头:
错误:......的冲突类型
由于您未按照声明的方式实现这些功能,例如您声明:
char *getinput();
但是这样实现:
getinput() {
char *output;
printf(" ");
scanf("%s", output);
return output;
}
假设返回值为int
。如果你这样修改你的实现:
char *getinput() {
...
它应该修复该错误。与该错误无关,您需要在output
中为getinput
分配内存:
char *output = malloc(...); //
否则你正在写一个未初始化的指针。
答案 2 :(得分:0)
默认返回类型为int
,因此getinput()
表示int getinput()
尝试改变:
#include "processcommand.h"
getinput() {
char *output;
printf(" ");
scanf("%s", output);
return output;
}
printoutput(char *input) {
printf("%s", input);
return 0;
}
getcommand() {
printoutput("Command:");
return getinput();
}
到
#include "processcommand.h"
char *getinput() {
char *output;
printf(" ");
scanf("%s", output);
return output;
}
unsigned char printoutput(char *input) {
printf("%s", input);
return 0;
}
char *getcommand() {
printoutput("Command:");
return getinput();
}