-
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)
{
}
主程序
#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 @@ yahqyentry @@ yahqay0mdfa > 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 @@ yahpaufpinfo @@ u1@utanker_record @@ z)错误LNK2019:函数 _main 中引用了未解析的外部符号“struct fpinfo * __cdecl InitHTable(struct fpinfo *)”(?InitHTable@@YAPAUfpinfo@@PAU1@@Z)
感谢您的帮助!!
解决方案
你包括 ht.cpp
从 main.cpp
, ,其中将包括已定义的函数的所有定义 ht.cpp
本身。
你想要包括 ht.h
反而。
在这种情况下它没有帮助,但您还应该使用包含防护来保护头文件:
#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 ^^^^^^^^^^^^^ ^^
其他提示
在标头中添加“包含防护”,以便其内容在预处理后不会“看到”两次。对于微软来说, #pragma once
在 .h 文件的开头。一般来说,添加:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
确保为每个标头采用一致的“唯一”命名方案。 __YOUR_HEADER_H
会做,例如 customio.h
进入 __CUSTOM_IO_H
.