gpt4 book ai didi

c++ - Unresolved external 错误

转载 作者:行者123 更新时间:2023-11-28 03:40:20 25 4
gpt4 key购买 nike

我有以下 .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>main.obj : error LNK2005: "struct fpinfo * __cdecl InitHTable(struct fpinfo (* const)[50000])" (?InitHTable@@YAPAUfpinfo@@QAY0MDFA@U1@@Z) already defined in ht.obj

1>main.obj : error LNK2005: "int __cdecl CreateTanker(struct tanker_record * const)" (?CreateTanker@@YAHQAUtanker_record@@@Z) already defined in ht.obj

1>main.obj : error LNK2005: "int __cdecl Hash_CreateEntry(struct fpinfo * (* const)[50000],struct fpinfo,struct tanker_record * const)" (?Hash_CreateEntry@@YAHQAY0MDFA@PAUfpinfo@@U1@QAUtanker_record@@@Z) already defined in ht.obj 1>main.obj : error LNK2005: "int curr_tanker" (?curr_tanker@@3HA) already defined in ht.obj 1>main.obj : error LNK2019: unresolved external symbol "int __cdecl Hash_CreateEntry(struct fpinfo *,struct fpinfo,struct tanker_record)" (?Hash_CreateEntry@@YAHPAUfpinfo@@U1@Utanker_record@@@Z) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "struct fpinfo * __cdecl InitHTable(struct fpinfo *)" (?InitHTable@@YAPAUfpinfo@@PAU1@@Z) referenced in function _main

感谢您的帮助!!

最佳答案

您正在从 main.cpp 中包含 ht.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 ^^^^^^^^^^^^^ ^^

关于c++ - Unresolved external 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9465294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com