【问题标题】:unresolved external errors未解决的外部错误
【发布时间】:2012-03-16 22:36:47
【问题描述】:

我有以下 .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)
{
}

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 : error LNK2005: "struct fpinfo * __cdecl InitHTable(struct fpinfo (* const)[50000])" (?InitHTable@@YAPAUfpinfo@@QAY0MDFA@U1@@Z) 已经在 ht 中定义。对象

1>main.obj:错误 LNK2005:“int __cdecl CreateTanker(struct tanker_record * const)” (?CreateTanker@@YAHQAUtanker_record@@@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:错误LNK2019:函数_main中引用的未解析的外部符号“struct fpinfo * __cdecl InitHTable(struct fpinfo *)”(?InitHTable@@YAPAUfpinfo@@PAU1@@Z)

感谢您的帮助!!

【问题讨论】:

    标签: c++ visual-c++ unresolved-external lnk2019 lnk2005


    【解决方案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                         ^^^^^^^^^^^^^                            ^^
    

    【讨论】:

    • 那是我最初所做的,但它仍然给出了最后三个错误。它只避免了前两个“lnk2005”错误
    • @John:哦,是的,当我阅读错误时,我的眼睛一定是呆滞的。这是因为声明与定义不匹配 - 声明具有指向对象的参数,而定义具有指向数组的参数。
    • 谢谢。但是函数调用的正确语法是什么?第一个创建错误 error C2664: 'InitHTable' : cannot convert parameter 1 from 'fpinfo *' to 'fpinfo *[][50000]' and error C2664: 'Hash_CreateEntry' : cannot convert将参数 1 从 'fpinfo *' 转换为 'fpinfo *[][50000]'
    • @John:看起来应该类似于Hash_CreateEntry(hash_table,fp,tr);。不过很难确定。函数定义中额外的* 似乎暗示hash_table 应该是一个二维指针数组,而不是对象。
    • 我遇到了另一个问题。它说函数 WriteHTtoFile() 已声明但未定义 可能是什么问题?我想这是某种参数类型不匹配,但无论我怎么看,我都无法弄清楚。
    【解决方案2】:

    在您的标头中添加一个“包含保护”,以便在预处理后它的内容不会“看到”两次。对于 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

    【讨论】:

    • 好主意,但你不应该使用reserved name,以防this happens
    • 当我按照你说的做时,我得到 error C2337: 'variable_name' : attribute not found 对于 ht.cpp 中使用的所有变量
    • 那是因为你在标题中定义了存储。标头应该只用于声明,最多用于常量。始终声明存储 - 将 extern int curr_tanker; 放在标题中,并将正确的 int curr_tanker; 放在“实现”.cpp 文件中。
    猜你喜欢
    • 2015-01-11
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2014-05-07
    • 2011-09-03
    • 2010-11-20
    相关资源
    最近更新 更多