0

I have the following .h and .cpp files

If i have to I will include the full codes of the function definitions

When i compile my program i get the errors shown at the end

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]);

i get the following errors when i try to compile it using vc2010

1>main.obj : error LNK2005: "struct fpinfo * __cdecl InitHTable(struct fpinfo (* const)[50000])" (?InitHTable@@YAPAUfpinfo@@QAY0MDFA@U1@@Z) already defined in ht.obj

1>main.obj : error LNK2005: "int __cdecl CreateTanker(struct tanker_record * const)" (?CreateTanker@@YAHQAUtanker_record@@@Z) already defined in 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) already defined in ht.obj 1>main.obj : error LNK2005: "int curr_tanker" (?curr_tanker@@3HA) already defined in ht.obj 1>main.obj : error LNK2019: unresolved external symbol "int __cdecl Hash_CreateEntry(struct fpinfo *,struct fpinfo,struct tanker_record)" (?Hash_CreateEntry@@YAHPAUfpinfo@@U1@Utanker_record@@@Z) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "struct fpinfo * __cdecl InitHTable(struct fpinfo *)" (?InitHTable@@YAPAUfpinfo@@PAU1@@Z) referenced in function _main

THANKS FOR YOUR HELP!!

pmg
  • 106,608
  • 13
  • 126
  • 198
John
  • 794
  • 2
  • 18
  • 34

2 Answers2

1

Add an "include guard" in your header, so that it its contents aren't "seen" twice after preprocessing. For Microsoft, #pragma once at the beginning of the .h file. In general, add:

#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif

Make sure to adopt a consistent "unique" naming scheme for each of your headers. __YOUR_HEADER_H would do, for example customio.h into __CUSTOM_IO_H.

foxx1337
  • 1,859
  • 3
  • 19
  • 23
  • 1
    Good idea, but you shouldn't use a [reserved name](http://stackoverflow.com/questions/228783), in case [this happens](http://stackoverflow.com/questions/3345159). – Mike Seymour Feb 27 '12 at 12:54
  • When i did as you said I get **error C2337: 'variable_name' : attribute not found** for all the variables used in ht.cpp – John Feb 27 '12 at 13:01
  • 1
    That, because you define storage in your header. Headers should only be for declarations, at most for constants. Always declare storage - put ``extern int curr_tanker;`` in the header and put the proper ``int curr_tanker;`` in the "implementation" .cpp file. – foxx1337 Feb 27 '12 at 13:03
1

You're including ht.cpp from main.cpp, which will include all the definitions of functions already defined in ht.cpp itself.

You want to include ht.h instead.

It won't help in this situation, but you should also protect the header file with include guards:

#ifndef HT_H
#define HT_H

// contents of ht.h

#endif

You also need the arguments of the function declarations to match those of the definitions:

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                         ^^^^^^^^^^^^^                            ^^
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • that was what i did originally but it still gives the last three errors. It only avoids the first two "lnk2005" errors – John Feb 27 '12 at 12:57
  • @John: Oh yes, my eyes must have glazed over as I read the errors. That's because the declarations don't match the definitions - the declarations have pointer-to-object arguments, while the definitions have pointer-to-array arguments. – Mike Seymour Feb 27 '12 at 13:02
  • Thanks. But what would be the right syntax for the function calls? The first ones create errors **error C2664: 'InitHTable' : cannot convert parameter 1 from 'fpinfo *' to 'fpinfo *[][50000]'** and **error C2664: 'Hash_CreateEntry' : cannot convert parameter 1 from 'fpinfo *' to 'fpinfo *[][50000]'** – John Feb 27 '12 at 13:18
  • @John: It looks like it should be something like `Hash_CreateEntry(hash_table,fp,tr);`. It's hard to be sure though; the extra `*` in the function definition seems to imply that `hash_table` should be a 2-dimensional array of pointers, not of objects. – Mike Seymour Feb 27 '12 at 13:23
  • i got another problem. It says the function **WriteHTtoFile() is declared but not defined** what could be the problem? I guess it is some kind of argument type mismatch but no matter how hard i look at it i couldn't figure it out. – John Feb 27 '12 at 14:12
  • @John: I guess it's being called from one of the other functions in that file. Functions have to be declared (or defined) before they are called - either move the definition of that file above the other functions, or put a declaration above them. – Mike Seymour Feb 27 '12 at 14:19