簡體   English   中英

C 多重定義/此處首次定義錯誤

[英]C Mulitple definition/first defined here error

我正在嘗試編寫一個書店程序,但在我的函數實現的源代碼文件中出現“多重定義”錯誤。

這是我的 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 的多重定義......首先在這里定義。

任何幫助或澄清將不勝感激!

不要這樣做:

#include “Book.c"

在您的 main.c 文件中。

解決辦法是刪除這一行:

#include "Book.c"

在 C 和 C++ 中,您通常只包含頭文件(.h 和 .hpp),因為您將 .c 和 .cpp 文件直接提供給編譯器,因此如果您也包含它們,則不明確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM