我有以下.h和.cpp文件
如果必须,我将包括函数定义的完整代码
当我编译我的程序时,我得到最后显示的错误
hash.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)
{
}
main.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> main.obj:错误LNK2005:“struct fpinfo * __cdecl InitHTable(struct fpinfo(* const)[50000])”(?InitHTable @@ YAPAUfpinfo @@ QAY0MDFA @ U1 @@ Z)已在ht.obj 1中定义> main.obj:错误LNK2005:“int __cdecl CreateTanker(struct tanker_record * const)”(?CreateTanker @@ YAHQAUtanker_record @@@ Z)已在ht.obj中定义1> main.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> main.obj中定义:错误LNK2005:“int curr_tanker”(?curr_tanker @@ 3HA)已在ht.obj中定义1> main.obj:错误LNK2019:未解析的外部符号“int __cdecl Hash_CreateEntry(struct fpinfo *,struct fpinfo,struct tanker_record)”(? Hash_CreateEntry @@ YAHPAUfpinfo @@ U1 @ Utanker_record @@@ Z)在函数_main 1> main.obj中引用:错误LNK2019:未解析的外部符号“struct fpinfo * __cdecl InitHTable(struct fpinfo *)”(?InitHTable @@ YAPAUfpinfo @@ PAU1 @@ Z)在函数_main中引用
谢谢你的帮助!!
2 回答
您将包含来自
main.cpp
的ht.cpp
,其中将包含已在ht.cpp
中定义的所有函数定义 .您想要包含
ht.h
.它在这种情况下无济于事,但您还应该使用包含警卫来保护头文件:
您还需要函数声明的参数来匹配定义的参数:
在 Headers 中添加"include guard",以便在预处理后其内容不是"seen"两次 . 对于Microsoft,
#pragma once
在.h文件的开头 . 一般来说,添加:确保为每个标头采用一致的"unique"命名方案 .
__YOUR_HEADER_H
会这样做,例如customio.h
进入__CUSTOM_IO_H
.