C++ 未解决的外部错误
我有以下.h和.cpp文件 如果有必要,我将包括函数定义的完整代码 当我编译我的程序时,我得到了最后显示的错误 hash.hC++ 未解决的外部错误,c++,visual-c++,unresolved-external,lnk2019,lnk2005,C++,Visual C++,Unresolved External,Lnk2019,Lnk2005,我有以下.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;
#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)
{
}
#包括
#包括
#包括
#包括
#包括“ht.h”
struct fpinfo*InitHTable(struct fpinfo ht[][B_条目])
{
}
int CreateTanker(油轮记录tr[])
{
}
int
Hash_CreateEntry(struct fpinfo*t[][B_ENTRIES],struct fpinfo he,carner_record tr[])
{
}
静态空隙
WriteHTtoFile(struct fpinfo*t[][B_ENTRIES],int this_)
{
}
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]);
#包括
#包括“ht.cpp”
#包括
#包括
void main(整型argc,字符**argv)
{
静态fpinfo散列_表[存储桶][B_项];
静态油轮记录tr[100];
InitHTable(&hash_表[0][0]);
油船(tr),;
结构fpinfo fp;
...
ar=Hash_CreateEntry(&Hash_table[0][0],fp,tr[0]);
当我尝试使用vc2010编译它时,会出现以下错误
1> main.obj:错误LNK2005:“结构fpinfo*u cdecl InitHTable(结构fpinfo(*const)[50000])”(?InitHTable@@YAPAUfpinfo@@QAY0MDFA@U1@@Z) 已在ht.obj中定义
1> main.obj:错误LNK2005:“int uu cdecl CreateTanker(struct tanker_record*const)”
已在ht.obj中定义了(?CreateTanker@@YAHQAUtanker_record@@@Z)
1> main.obj:错误LNK2005:“int uu cdecl Hash_CreateEntry(struct fpinfo*(*const)[50000],struct fpinfo,struct carner_record*const)”(?Hash_CreateEntry@@YAHQAY0MDFA@PAUfpinfo@@U1@QAUtanker_record@@@Z) 已在ht.obj中定义
1> main.obj:错误LNK2005:“int curr_carner”(?curr_carner@@3HA)已在ht.obj中定义
1> main.obj:错误LNK2019:未解析的外部符号“int\uu cdecl Hash\u CreateEntry(struct fpinfo*,struct fpinfo,struct carner\u record)”
(?Hash_CreateEntry@@YAHPAUfpinfo)@@U1@Utanker_record@@@Z) 在函数_main中引用
1> main.obj:错误LNK2019:未解析的外部符号“struct fpinfo*\uu cdecl InitHTable(struct fpinfo*)”(?InitHTable@@YAPAUfpinfo@@PAU1@@Z)在函数\u main中引用
感谢您的帮助!!在头文件中添加一个“include guard”,这样在预处理后它的内容就不会被“看到”两次。对于Microsoft,在.h文件的开头添加一次pragma。通常,添加:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
确保采用一致的“唯一”每个标题的命名方案。
\u您的标题可以,例如customio.H
到\u CUSTOM\u IO\H
中,包括ht.cpp
中的main.cpp
,其中包括ht.cpp
本身已定义的所有函数定义
您希望包含ht.h
在这种情况下,它不会有帮助,但您还应该使用include-guard保护头文件:
#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 ^^^^^^^^^^^^^ ^^
好主意,但你不应该使用,以防万一。当我按照你说的那样做时,我得到了错误C2337:“variable\u name”:没有为ht.cppThat中使用的所有变量找到属性,因为你在头中定义了存储。头应该只用于声明,最多用于常量。始终在h中声明存储-putextern int curr\u;
读取并在“implementation”.cpp文件中放入适当的int curr\u;
。这是我最初做的,但它仍然给出了最后三个错误。它只避免了前两个“lnk2005” errors@John:哦,是的,当我读到错误时,我的眼睛一定是呆滞了。这是因为声明与定义不匹配-声明具有指向对象参数的指针,而定义具有指向数组参数的指针。谢谢。但是函数调用的正确语法是什么?第一个声明创建错误错误C2664:“InitHTable”:无法将参数1从“fpinfo*”转换为“fpinfo*[][50000]”,错误C2664:“Hash_CreateEntry”:无法将参数1从“fpinfo*”转换为“fpinfo*[][50000]”@John:看起来应该类似于Hash_CreateEntry(Hash_table,fp,tr)
。但是很难确定;函数定义中额外的*
似乎暗示哈希表应该是指针的二维数组,而不是对象的数组。我遇到了另一个问题。它说函数writehtofile()是声明的,但没有定义什么问题?我猜这是某种参数类型不匹配,但不管我怎么看,我都无法找出它。