-2

I have 5 files that I'm trying to link together; 2 header files with class declarations, 2 cpp files with Class definitions, and 1 file with the main method

Header File 1: city.h

#ifndef CITY_H
#define CITY_H

class City
{
// Class header code
}
#endif

Definition File 1: city.cpp

#include "City.h
#include <iostream>
#include <string.h>

#ifndef CITY_H
#define CITY_H
//Class definition code
#endif

This is where I'm getting an error when compiling

Header File 2: hash.h

#include "City.h
#include <iostream>
#include <string.h>

#ifndef CITY_H
#define CITY_H
//Class definition code
#endif

Definition File 2: hash.h

#include "Hash.h"
#include <iostream>
#include <string.h>

#ifndef HASH_H
#define HASH_H
//Class definition
#endif

.cpp File with Main Method

#include "Hash.cpp"
#include "City.cpp"

using namespace std;
int main()
{
    int size;
    Hash* table = new Hash(size);

    return 0;
}

When I compile with g++ I get the following error:

/tmp/ccXGXFpg.o: In function 'main':
Project1.cpp:(.text+0x28): undefined reference to 'Hash::Hash(int)'
collect2: error: 1d returned 1 exit status

I'm really not sure what's wrong. The constructor for Hash is overloaded so there is a Hash::Hash() and Hash::Hash(int) definition. I've been trying to figure it out but I'm beat. Any help is much appreciated, thank you!

user4581301
  • 33,082
  • 7
  • 33
  • 54
yuutt66
  • 7
  • 3
  • Please provide a [mcve]. This includes but isn't limited to: **your code** and how you call the compiler – Passer By Feb 27 '18 at 05:20
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Passer By Feb 27 '18 at 05:21
  • Unrelated: including cpp files is a bad, bad idea. Unlikely to be related to your current problem, but it often results in duplicate definitions as everything in City.cpp and Hash.cpp is now duplicated in the main cpp. Compiling City.cpp and Hash.cpp and linking them with the main cpp, what you are supposed to do with cpp files, will result in 2 copies of everything in City.cpp and Hash.cpp being found twice. Include headers. Compile and link cpp files. – user4581301 Feb 27 '18 at 06:05
  • Anyway, your actual problem cannot be conclusively solved with the information provided. Voting to close. – user4581301 Feb 27 '18 at 06:06
  • Remove `#ifndef` , `#define` and `#undef` directives from `.cpp` files. – rafix07 Feb 27 '18 at 06:08

1 Answers1

1

You should not use #ifndef directive in .cpp file. I suppose your hash.h file has content

#include "City.h
#include <iostream>
#include <string.h>

#ifndef HASH_H
#define HASH_H
//Class definition code
#endif

so when hash.cpp file is compiled, the code of which is

#include "Hash.h"   // [1]
#include <iostream>
#include <string.h>

#ifndef HASH_H   // [2]
#define HASH_H
//Class definition
hash::hash () {
     // ctor definition
}
#endif

in [1] line content of header file was included to .cpp file, and HASH_H macro was defined. In [2] line #ifndef directive checks whether HASH_H is defined, if so, all definitions of your hash class members are ignored and you get undefined reference error when linker works.

If you compile main.cpp, hash.cpp and city.cpp separately, in main.cpp you should put

#include "city.h"
#include "hash.h"

instead of

#include "city.cpp"
#include "hash.cpp"

without this you will get multiple definitions error.

rafix07
  • 20,001
  • 3
  • 20
  • 33