2012-02-27 70 views
0

我有以下的.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:“结构fpinfo * __cdecl InitHTable(结构已经在ht.obj中定义了fpinfo(* const)[50000])“(?InitHTable @@ YAPAUfpinfo @@ QAY0MDFA @ U1 @@ Z)

1> main.obj:error LNK2005:”int __cdecl CreateTanker tanker_record * const)“ (?CreateTanker @@ YAHQAUtanker_rec ord @@@ Z)已经在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)已经在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:error LNK2019:无法解析的外部符号“struct fpinfo * __cdecl InitHTable(struct fpinfo *)”(?InitHTable @@ YAPAUfpinfo @@ PAU1 @@ Z)主

感谢您的帮助!

回答

1

您在包括main.cppht.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       ^^^^^^^^^^^^^       ^^ 
+0

这是我最初做的,但它仍然给最后三个错误。它只能避免前两个“lnk2005”的错误 – John 2012-02-27 12:57:46

+0

@John:哦,是的,当我读错误时,我的眼睛一定已经看到了。这是因为声明与定义不匹配 - 声明具有指向对象的参数,而定义具有指向数组的指针。 – 2012-02-27 13:02:46

+0

谢谢。但是函数调用的正确语法是什么?第一个创建错误**错误C2664:'InitHTable':无法将参数1从'fpinfo *'转换为'fpinfo * [] [50000]'**和**错误C2664:'Hash_CreateEntry':无法将参数1从'fpinfo *'到'fpinfo * [] [50000]'** – John 2012-02-27 13:18:15

1

在标题中添加一个“包含警卫”,这样它的内容在预处理后不会“看到”两次。对于Microsoft,#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

+1

好主意,但你不应该使用[保留名称](http://stackoverflow.com/questions/228783),以防[发生这种情况](http://stackoverflow.com/questions/3345159) )。 – 2012-02-27 12:54:06

+0

当我按照你说的那样得到**错误C2337:'variable_name':属性找不到**对于ht.cpp中使用的所有变量 – John 2012-02-27 13:01:13

+1

这是因为你在头文件中定义了存储。标题只能用于声明,最多用于常量。总是声明存储 - 在头文件中放置''extern int curr_tanker;''并在“implementation”.cpp文件中放入正确的''int curr_tanker;''。 – foxx1337 2012-02-27 13:03:52

相关问题