- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了奇怪的错误,其中大多数涉及第 9-12 行(函数声明),但我找不到问题。请帮助我:) 谢谢
代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256
void Get_Lost(char* str);
void input_book(Book* B, FILE *in);
void input_library(Library *L, FILE *in);
void output_book(Book* B, FILE *out);
void output_library(Library* L, FILE *out);
typedef struct Book
{
char book_code[10];
char *book_name;
}Book;
typedef struct Library
{
char library_name[MAX];
int num_books;
Book *B;
}Library;
int index = 0;//global variable to check the number of 'enters' n the input book function
int main()
{
FILE *in, *out;
Library Libr;
input_library(&Libr, in);
fclose(in);
output_library(&Libr, out);
fclose(out);
//for (i = 0; i<Libr.num_books; i++)
return 0;
}
void input_book(Book* B, FILE *in)
{
int counter = 0; //counts the number of enters (\n) to identify the book_code and book_name
char temp_book_name[MAX];
in = fopen("input.txt", "rt");
if (in == NULL)
{
Get_Lost("File not found");
}
while (counter < 2 + index)
{
if (getc(in) == '\n')
counter++;
in++;
}
index++; //increasing the index for the next line
fgets(B->book_code, MAX, in); //put the book code in the structure
fgets(temp_book_name, MAX, in);//puts the book name in temprary var, in order to allocate memory dynamically afterwards
B->book_name = (char *)malloc(strlen(temp_book_name) * sizeof(char));/*dynamic allocation of memory
for the book name using temp var*/
if (B->book_name == NULL)
{
Get_Lost("No memory");
}
B->book_name = temp_book_name;
fclose(in);
}
void input_library(Library *L, FILE *in)
{
int j;
Book B;
in = fopen("input.txt", "rt");
if (in == NULL)
{
Get_Lost("File not found");
}
fgets(L->library_name, MAX, in);
fgets(L->num_books, MAX, in);
L->B = (Book *)malloc(L->num_books * sizeof(Book));//dynamic allocation for the array of books in the library
for (j = 0; j < L->num_books; j++)
{
input_book(&L->B[j], in);//calling the input_book function 'num_books' times to fill all the Books
}
fclose(in);
}
void output_book(Book* B, FILE *out)
{
out = fopen("input.txt", "wt");
if (out == NULL)
{
Get_Lost("File not found");
}
fprintf(out, "%s ", B->book_code);
fprintf(out, "%s\n", B->book_name);
fclose(out);
}
void output_library(Library* L, FILE *out)
{
int k;
Book B;
out = fopen("input.txt", "wt");
if (out == NULL)
{
Get_Lost("File not found");
}
fprintf(out, "%s\n", L->library_name);
for (k = 0; k < L->num_books; k++)
{
output_book(&L->B[k], out);//calling the output_book function 'num_books' times to print the books' details
}
fclose(out);
}
void Get_Lost(char* str)
{
printf("\n%s", str);
exit(1);
}
错误:
C2059: syntax error : ')' LINE 9
Error 10 error C2059: syntax error : ')' LINE 10
Error 15 error C2059: syntax error : ')' LINE 11
Error 20 error C2059: syntax error : ')' LINE 12
Error 1 error C2143: syntax error : missing ')' before '*' LINE 9
Error 6 error C2143: syntax error : missing ')' before '*' LINE 10
Error 11 error C2143: syntax error : missing ')' before '*' LINE 11
Error 16 error C2143: syntax error : missing ')' before '*' LINE 12
Error 4 error C2143: syntax error : missing ';' before '*' LINE 9
Error 9 error C2143: syntax error : missing ';' before '*' LINE 10
Error 14 error C2143: syntax error : missing ';' before '*' LINE 11
Error 19 error C2143: syntax error : missing ';' before '*' LINE 12
Error 2 error C2143: syntax error : missing '{' before '*' LINE 9
Error 7 error C2143: syntax error : missing '{' before '*' LINE 10
Error 12 error C2143: syntax error : missing '{' before '*' LINE 11
Error 17 error C2143: syntax error : missing '{' before '*' LINE 12
C2371: 'FILE' : redefinition; different basic types LINE 9
C2371: 'FILE' : redefinition; different basic types LINE 10
C2371: 'FILE' : redefinition; different basic types LINE 11
C2371: 'FILE' : redefinition; different basic types LINE 12
C2371: 'input_library' : redefinition; different basic types LINE 73
C2371: 'output_library' : redefinition; different basic types LINE 105
最佳答案
移动结构定义,使其位于函数声明之前。另外,我的编译器不喜欢使用索引作为变量名,因为它是在 string.h
中定义的。
还给出了一些警告,您应该在函数调用中使用in
和out
之前初始化它们以消除警告。您可以像这样 File *in = NULL, *out = NULL;
执行此操作,fgets
也需要一个 char*
但您传递了 L->num_books
这是一个 int
。
关于c - C 中有很多错误, "redefinition"错误等等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560311/
我正在尝试制作一个压缩和解压缩文件的程序。我想为操作类型创建这样的枚举:typedef enum { COMPRESS, DECOMPRESS } operation;。问题是,我收到 4 个错误:
此代码会导致编译错误“错误:使用不同类型重新定义'p'”: void fun() { printf("fun"); } void (*p)(); p = &fun; 但是如果修改 void (
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
这个问题在这里已经有了答案: Avoiding "redefinition of typedef" warnings when defining structs (2 个答案) 关闭 5 年前。 我
对于以下使用 Visual C++ 2010 的代码,我有一个奇怪的编译警告: #include class test { public: template
我有两个库(第三方),在每个库中,它们都定义了两个同名的类(在头文件中)。 // Lib A, HeaderA.h struct mycompare { //Some code }; // Lib B
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Imp
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在处理类里面的一个问题。它是关于创建一个“addressType”类,并使用它与已经写出的主函数进行交互。该程序无法运行,我得到的主要错误是“addressType.cpp:6:7: error:
我的 C++ 代码有一些问题。我现在正在尝试拆分一个头文件,该文件具有一些 char 指针的定义和声明(以前是字符串,但我遇到了一些 CRT 问题,因此将它们更改为 char 指针)。由于我已将它们更
我已经很久不用C++了,对它不是很友好。我正在恢复我的一个旧项目并尝试编译它,但我在启动 Makefile 时收到以下错误输出: g++ -W -ggdb3 -Wall -ansi -pedantic
我正在尝试构建一个链表,但我一开始就卡住了——我是一个完全的初学者。如何定义结构? 这是我收到的错误消息: 8 8 C:\Users\user\Desktop\list_funcs.c [错误] 's
这是我的代码: // in main.cpp #include "iostream" #include "circle.cpp" #include "rectangle.cpp" #include "
我有一个 iOS xcode 项目,当我选择 Generic iOS Device 作为我的目的地时,它构建得很好。但是,当我选择任何模拟器选项时,我的构建失败并出现许多 Redefinition o
我正在尝试将用户空间库链接到 Windows 内核驱动程序。它引用了 __iob_func,它是“libcmt.lib”(用户空间库)的一部分。我无法在 winddk 中访问此功能。因此,我计划为 _
在下面的代码中: typedef struct { union U { int a; char b; }U1;
我正在做一个大项目,我收到这个警告: ...\include\stddef.h" 38/9] macro "NULL" redefined ...\ncs_types.h" 125/13] previ
我在这里有这段代码,并且出现了GCC错误:在此代码的开头重新定义了union semun。 union semun { int val; /* Value fo
我是 javascript/typescript 开发人员,但对 Arduino/c++ 很陌生 我有一个类(见下面的 h 和 cpp)并且有这个编译器错误: DotMatrix.cpp:13:1:
我遇到了奇怪的错误,其中大多数涉及第 9-12 行(函数声明),但我找不到问题。请帮助我:) 谢谢 代码: #include #include #include #include #defin
我是一名优秀的程序员,十分优秀!