3

我正在尝试编写一个书店程序,并且在我的函数实现中的源代码文件中出现“多重定义”的错误。

这是我的 Book.c 文件:

#include "Book.h"

void scanBook(book_t* bk) //error here
{
    //implementation here
}

这是我的 Book.h 文件:

#pragma once
#include <stdio.h>

typedef char* str;    
typedef enum Genre {Education, Business, Novel} genre_t;

typedef struct Book{
    str ISBN;
    str title;
    str author;
    float price;
    int quantity;
    genre_t genre;
} book_t;

void scanBook(book_t* bk);

这是我的 main.c 文件:

#include "Book.h"
#include "Book.c"

int main()
{
    return 0;
}

错误发生在 Book.c 中的 scanBook 函数中,但我不知道为什么,因为我包含了头文件以及 #pragma 一次,并且在头文件中我声明了该函数。它说 'scanBook' 和 obj\Debug\Book.o .... 的多个定义首先在这里定义。

任何帮助或澄清将不胜感激!

4

2 回答 2

7

不要这样做:

#include “Book.c"

在你的 main.c 文件中。

于 2014-10-24T19:43:46.400 回答
0

解决方案是删除这一行:

#include "Book.c"

在 C 和 C++ 中,通常您只包含头文件(.h 和 .hpp),因为您将 .c 和 .cpp 文件直接提供给编译器,因此如果您还包含它们,则不明确。

于 2020-08-04T08:45:36.320 回答