r/learnprogramming•
ams152

[C++] Having trouble with .cpp and .hpp files. Which do I include?

I remember being told not to include .cpp files and only include the .hpp files. However when I do something like this:

#include "randomFile.hpp"
int main(){
	//Try to call a method from randomFile
	return 0;
}

I get an error saying that the method is undefined. Which makes sense because it's defined in the .cpp file. So I try this:

#include "randomFile.cpp"
int main(){
	//Try to call a method from randomFile
	return 0;
}

And I get an error saying that the class randomFile is being redefined in the .cpp file (which includes the .hpp file). So the only solution I can see is to get rid of the .hpp file. However, if that was the case then what is the purpose of having the .hpp file anyways and why do people say not to include a .cpp file?

I have a feeling I'm screwing something up somewhere. Can someone explain to me the proper way to include .cpp and .hpp files in conjunction with one another? Where do I include each one?

Posted Dec 2, 2015, 11:47 PM UTC

211
Loading comments…