-
13-11-2019 - |
質問
私は次のものを持っています。hと.cppファイル
必要に応じて、関数定義の完全なコードを含めます
私のプログラムをコンパイルすると、最後にエラーが表示されます
ハッシュh
#define BUCKETS 64
#define B_ENTRIES 50000
int curr_tanker;
typedef unsigned long int ulong;
typedef struct bucket
{
int bucket_id;
ulong bucket_entries;
}bucket;
typedef struct tanker_record
{
ulong tanker_id;
ulong tanker_size;
ulong num_of_entries;
ulong bucket_entry_count;
}tanker_record;
typedef struct fpinfo
{
unsigned long chunk_offset;
unsigned long chunk_length;
unsigned char fing_print[33];
}fpinfo;
struct fpinfo* InitHTable(fpinfo *);
int CreateTanker(tanker_record tr[]);
int Hash_CreateEntry(struct fpinfo *,struct fpinfo he,tanker_record tr);
ht.cpp
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include <iostream>
#include "ht.h"
struct fpinfo* InitHTable(struct fpinfo ht[][B_ENTRIES])
{
}
int CreateTanker(tanker_record tr[])
{
}
int
Hash_CreateEntry(struct fpinfo *t[][B_ENTRIES],struct fpinfo he,tanker_record tr[])
{
}
static void
WriteHTtoFile(struct fpinfo *t[][B_ENTRIES],int this_tanker)
{
}
メイン...cpp
#include<iostream>
#include"ht.cpp"
#include<conio.h>
#include<stdlib.h>
void main(int argc, char **argv)
{
static fpinfo hash_table[BUCKETS][B_ENTRIES];
static tanker_record tr[100];
InitHTable(&hash_table[0][0]);
CreateTanker(tr);
struct fpinfo fp;
...
ar = Hash_CreateEntry(&hash_table[0][0], fp,tr[0]);
vc2010を使用してコンパイルしようとすると、次のエラーが発生します
1>メイン。obj :エラー LNK2005:"struct fpinfo*__cdecl InitHTable(struct fpinfo(*const)[50000])"(?InitHTable@@YAPAUfpinfo@@QAY0MDFA@U1@@Z)すでにhtで定義されています。obj
1>メイン。obj :エラー LNK2005:"int__cdecl CreateTanker(struct tanker_record*const)" (?CreateTanker@@Yahqautanker_Record@@@Z)すでにhtで定義されています。obj
1>メイン。obj :エラー LNK2005:"int__cdecl Hash_Createentry(struct fpinfo*(*const)[50000],struct fpinfo,struct tanker_record*const)"(?Hash_createentry@@YAHQAY0MDFA@PAUfpinfo@@U1@Qautanker_Record@@@Z)すでにhtで定義されています。obj 1>メイン。obj :エラー LNK2005:"int curr_tanker"(?curr_tanker@@3HA)すでにhtで定義されています。obj 1>メイン。obj :エラー LNK2019:未解決の外部シンボル"int__cdecl Hash_Createentry(struct fpinfo*,struct fpinfo,struct tanker_record)" (?Hash_createentry@@YAHPAUfpinfo@@U1@Utanker_Record@@@Z)関数_mainで参照されています 1>メイン。obj :エラー LNK2019:未解決の外部シンボル"struct fpinfo*__cdecl InitHTable(struct fpinfo*)"(?InitHTable@@YAPAUfpinfo@@PAU1@@Z)関数_mainで参照されています
あなたの助けをありがとう!!
解決
あなたは含んでいます ht.cpp
から main.cpp
, で既に定義されている関数のすべての定義が含まれます ht.cpp
それ自体。
あなたが含めたい ht.h
代わりに。
このような状況では役に立ちませんが、ヘッダファイルをinclude guardsで保護する必要があります:
#ifndef HT_H
#define HT_H
// contents of ht.h
#endif
また、関数宣言の引数が定義の引数と一致する必要があります:
struct fpinfo* InitHTable(struct fpinfo[][B_ENTRIES]);
// Missing: ^^^^^^^^^^^
int CreateTanker(tanker_record tr[]); // OK
int Hash_CreateEntry(struct fpinfo*[][B_ENTRIES],struct fpinfo,tanker_record[]);
// Missing ^^^^^^^^^^^^^ ^^
他のヒント
ヘッダーに「包含ガード」を追加するので、その内容が前処理後2回「見て」されません。Microsoftの場合、.hファイルの先頭にある#pragma once
の場合。一般的に、追加:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
.
あなたの各ヘッダーには、一貫した「ユニークな」命名方式を必ず採用してください。__YOUR_HEADER_H
は、例えばcustomio.h
に__CUSTOM_IO_H
を実行します。