Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to include .CPP files (implementation of classes) in the project ?

Status
Not open for further replies.

micha12

Programmer
Dec 11, 2002
10
RU
Hello,

Suppose I have created a class, with two files: MyClass.h and MyClass.cpp. Then I have started a new project, and want to use the class in the project. I can easily include the MyClass.h file by just typing #include "MyClass.h" in my project.

But how should I include the .cpp file? Or maybe in this case I should create only one file for my class, MyClass.h, which would contain the declaration as well as the implementation of the class? But if the file is included several times, I will get a compiler error, I suppose, since the same functions (method of the class) will be implemented several times.

So, my question is, how do I include .cpp files in a new project?

And could anybody recommend a book or better a web page explaining all these settings (how to include files, link libraries, ...)

Thanks in advance for help,
Mikhail.
 
right click on the project explorer(in the right or left or VisualC++), choose add existing or new....., broswe for your cpp and add it. You will see all sources in the project explorer. All sources you're using is highly recommended to be in the same directory with your project

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Thanks for the answer.

I know the procedure you described. But aren't there more "pretty" ways to add .cpp files? Like typing something like #include "XXX.h" fo header files? It is not pretty to see all .cpp dependencies in the Project Explorer! I want them to be connected, but not visible.

Maybe you'll be able to help me. Thanks again.
 
>> It is not pretty to see all .cpp dependencies in the
>> Project Explorer! I want them to be connected, but not
>> visible.

Then collapse the tree node that they are in. Poof, not visible right?


-pete
 
sorry, micha12, what you say makes no sence. All source, resource, idl and header files must be visible. Also to #include .cpp or .c files does not make sence because they are sources, not headers.


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Hello,

Yesterday I opened an MFC header file and found that it also contained the implementation of all the methods! If I am not mistaken, it was the file with the CArray class. How did they achieve that? If you want to use the MFC class, all you have to do is to write #include "MFCHeader.h", and you can work with the class. And when your project compiles, there are no errors connected with the redefinition of the functions and so on.

Thanks for your replies.
 
>> How did they achieve that?

If you were looking at a template class, (I think CArray is a template right?),templates are implemented in header files. Compilers (at least most of them) do not yet support template definitions in implementation files (cpp, cc)


-pete
 
To Quote IonFillipski
"All sources you're using is highly recommended to be in the same directory with your project."

This makes your file management easier, but you must be careful with version control if you implement this and change your core code, else you will have different classes that do different things, but with the same name.

K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top