Abhilash D K
Abhilash D K

Reputation: 1309

Redefinition of Class in C++

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.

Upvotes: 0

Views: 273

Answers (2)

DevO
DevO

Reputation: 375

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.

Upvotes: 1

maxrt
maxrt

Reputation: 19

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.

Upvotes: 0

Related Questions