Erreurs externes non résolues
-
13-11-2019 - |
Question
J'ai les fichiers .h et .cpp suivants
Si je dois, j'inclurai les codes complets des définitions de fonction
Quand je compile mon programme, je reçois les erreurs affichées à la fin
hachage
#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]);
J'obtiens les erreurs suivantes lorsque j'essaie de le compiler en utilisant VC2010
1> main.obj: erreur lnk2005: "struct fpinfo * __cdecl InithTable (struct fpinfo (* const) [50000])" (? InithTable @@ yapaufpinfo @@ qay0mdfa @ u1 @@ z) déjà défini en ht.obj
1> main.obj: erreur lnk2005: "int __cdecl createTanker (struct tanker_record * const)" (? CreateTanker @@ yahqautanker_record @@@ z) déjà défini dans ht.obj
1> main.obj: erreur lnk2005: "int __cdecl hash_createentry (struct fpinfo * (* const) [50000], struct fpinfo, struct tanker_record * const)" (? Hash_createentry @@ yahqay0mdfa @ paufpinfo @@ u1 @ qautanker_record @@@@@wing Z) déjà défini dans ht.obj 1> main.obj: error lnk2005: "int curr_tanker" (? Curr_tanker @@ 3ha) déjà défini dans ht.obj 1> main.obj: error lnk2019: un symbole externe non résolu "int __cdecl hash_createy (struct fpinfo *, struct fpinfo, struct tanker_record) "(? hash_createentry @@ yahpaufpinfo @@ u1 @ utanker_record @@@ z) Référencé en fonction _main 1> main.obj: error lnk2019: symbole externe non résolu" (struct fpinfo *) "(? inithtable @@ yapaufpinfo @@ Pau1 @@ z) Référencé en fonction _main
MERCI POUR VOTRE AIDE!!
La solution
Vous incluez ht.cpp
de main.cpp
, qui comprendra toutes les définitions des fonctions déjà définies dans ht.cpp
lui-même.
Vous voulez inclure ht.h
Au lieu.
Cela ne vous aidera pas dans cette situation, mais vous devez également protéger le fichier d'en-tête avec Inclure Guards:
#ifndef HT_H
#define HT_H
// contents of ht.h
#endif
Vous avez également besoin des arguments des déclarations de fonctions pour correspondre à celles des définitions:
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 ^^^^^^^^^^^^^ ^^
Autres conseils
Ajoutez un "gardien d'inclusion" dans votre en-tête, afin que son contenu ne soit pas "vu" deux fois après le prétraitement. Pour Microsoft, #pragma once
au début du fichier .h. En général, ajouter:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
Assurez-vous d'adopter un schéma de dénomination cohérent "unique" pour chacun de vos en-têtes. __YOUR_HEADER_H
ferait, par exemple customio.h
dans __CUSTOM_IO_H
.