0

I am new to C++. I am learning Data Structures and Algorithms and I have created a HashMap class in C++. I am trying to use this HashMap map to solve two separate problems, which I have written in two separate class files. Here is an image of my source directory:

enter image description here

I am including HashMap class in ArrayIsASubsetOfAnotherArray.cpp and ArrayDisJointOrNot.cpp and I am including these two files in my main.cpp file:

enter image description here

enter image description here

As you can see in the above image I am getting Redefinition of 'HashMap' error.

Why I am getting this error. What is the solution for this problem. I am trying to use the same HashMap and HashNode classes in all the problems i solve.

Thanks in Advance.

Abhilash D K
  • 1,223
  • 1
  • 23
  • 39
  • 1
    1) Don't post code as screenshots, paste code to the question. 2) You need to split your code to `.h` and `.cpp` https://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files – fas Apr 10 '20 at 09:51
  • @fas 1) I wanted to show the error my editor was throwing so included screen shots. 2) This the link you provided explained the concept clearly. I will split my code into .h and .cpp files. – Abhilash D K Apr 10 '20 at 09:56

2 Answers2

1

Do not include *.cpp files! You need to put HashMap class declaration into *.hpp (or *.h) files and include only this into main.cpp.

Interface of HashMap should fo into HashMap.hpp and implementation into HashMap.cpp.

DevO
  • 365
  • 1
  • 8
  • Thanks for the answer I am trying to do the same now. – Abhilash D K Apr 10 '20 at 10:15
  • @AbhilashDK comments should only be used to clarify problems regarding that answer (or question). If an answer is solving your problem, then give it an upvote, and accept the answer that describes the solution best. – t.niese Apr 10 '20 at 10:17
0

I think the problem is that you are including HashMap.cpp in both ArrayIsASubsetOfAnotherArray.cpp and ArrayDisJointOrNot.cpp. #include basically just copy pastes code, so try to remove one #include "HashMap.cpp" from ArrayIsASubsetOfAnotherArray.cpp or ArrayDisJointOrNot.cpp. Or add #pragma once into HashMap.cpp.

maxrt
  • 19
  • 4