Classes & .h / .cpp files

Hi folks,

After taking a couple of weeks away from C++ in order to move house, i'm back with renewed vigour / curiosity!

A question regarding the implementation of classes using .h and .cpp files.

I've been going over this lesson from Learncpp.com again:
http://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/

At the end of the lesson Alex states:
"Note that Date.cpp also needs to be compiled into any project that uses Date.h so the linker knows how Date is implemented."

But he doesn't say how to ensure this takes place. Could anyone explain to me how that works? Or is it as simple as the compiler includes any .cpp file that is in the same folder as your main program .cpp?

Thanks! ............D

Last edited on
closed account (48T7M4Gy)
1
2
3
4
//Date.cpp:

	
#include "Date.h" 



1
2
3
4
5
6
7
8
//Program.cpp:
	
#include "Date.h"

int main()
{
// Date date ...
}
Last edited on
You need to add the files wich a supposed to be compiled to the project (if you use an IDE) or makefile. They are not automatically included in the build process.
@coder777:

OK! When you say "add", i presume you mean literally use the command "add" in my IDE? (i'm in VS2015).

But that still leaves me a little confused. Because by doing that, all i have done is create a .cpp that lives in the same directory as my other solution files. Which makes me assume that the compiler will include any .cpp that is in that folder.

But you say that is not the case. So if (for the sake of argument) i just copy / paste another .cpp to that folder, that wouldn't be included? If not, then where is this list of .cpp files to be included?

If what i just wrote is totally nonsensical, appologies!
Only files that are listed in the projects are compiled. You can remove them from your project (they still exists in that folder).

i just copy / paste another .cpp to that folder, that wouldn't be included?
Correct. Solely because a file exists (no matter where) doesn't mean it will be compiled.

If not, then where is this list of .cpp files to be included?
One way is it to drag/drop those files in question into your project.

If you create a project some files may automatically included.
Thanks!

You said: "Only files that are listed in the projects are compiled."

Where can i find / view that list? It's listed in the window on the left of VS2015, is that what you mean? But still i'd like to know, where is it actually listed (in which file)?!
Last edited on
Hi,

Somewhere in the menu, there is an option to create a new class, when you do that both the header file and the cpp file will automagically be added to your project.

I like to use *.hpp for headers, it means that they contain c++ code, whereas .h may imply C code.

But still i'd like to know, where is it actually listed (in which file)?!


There should be a pane to turn on, that has all of your source, headers targets etc - I have an idea it's on by default, and should be on the left of your screen.
the tree that vs shows you in "Solution Explorer" of your files looks like folders, but they aren't. they are filters. right click one and look at its properties, you will see the file extensions that vs will include in that filter.

to reuse files in another vs project,

1. use windows explorer to copy the files into the new projects folders so they sit with the new projects source code.

2. in "solution explorer" right click the project and choose "Add/Existing Item" and select both .h and cpp files, and click the Add button.

if vs is configured correctly the .h file should appear in the "Header Files" filter, and the cpp should appear in the "Source Files" filter.

if vs is not configured correctly you can drag/drop them into the filter you wish without affecting where the files re actually stored.
Topic archived. No new replies allowed.