2012-02-27 92 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

相關問題