Where do I put my class functions .cpp??

Started by
8 comments, last by Tevong 22 years, 6 months ago
So I declare my class in classdef.h but where do I put clsfunctions.cpp which contains the class functions?? Stupid book tells me to make a .h header file a clsfunc.cpp containing the functions and calls the class function in a program without actually telling me how clsfunc.cpp should be linked with the classdef.h
Signed: ___T____
Advertisement
If your class declarations are in classdef.h and your definitions are in clsfunctions.cpp then you need to put
#include "classdef.h"  
at the top of your clsfunctions.cpp file.

Edited by - Red Ant on October 16, 2001 1:39:02 PM
I did but where do I actually put clsfunc.cpp?? Do I put it in my source files folder, in my INCLUDE folder? At the moment I tried putting it in INCLUDE folder or the same folder as my source code, and the classdef.h in INCLUDE folder but it says can''t find functions.
Signed: ___T____
Sorry, I''m not following you. What do you mean what folder do you put it in?? :confused: All your files (both .cpp and .h) go in whatever folder your project is resident in. You don''t have to create any special folders for .h files and .cpp files.
If you declare functions in a header file, you only need to define them (fill their bodies) in one source file - and you can spread them out in a bunch of source files. When you compile your files, your compiler will resolve every declaration and definition.

If you have another source file that includes the header and calls the function, then the compiler needs to know where both source files are (usually they''re in the same folder, but you can add a source file from anywhere to the project).

I''d suggest you read your compiler documentation and search the net for a few project building tutorials (it took me a few weeks/months to figure them out when I started too )
That''s what had me confused, how does the compiler know where the functions are?

"If you have another source file that includes the header and calls the function, then the compiler needs to know where both source files are "

Exactly what I mean, how does it know where to search for functions in classdef.h there is no reference to clsfunc.cpp at all...
Signed: ___T____
Actually C++ compilation process is done in three stages.

First stage is preprocessing. This is where header files are included into cpp files.

In second stage compiler compiles each cpp file and puts the code into a separate object file. When compiler finds a reference to some function that is not defined in this cpp file it doesn't search for this function, it only adds its name to an object file as an external reference.

In third stage the object files are linked together to make a single executable file and all references are resolved (this stage uses only object files). If some function cannot be found, you get a linking error.


In fact, you can even create a program without any header files:
  //File aaa.cpp#include <cstdio>void func(int a) // definition of func{  printf("a = %i\n", a);}//End of file aaa.cpp  

  //File bbb.cppvoid func(int);int main(){  func(55); //ok, because compiler see its declaration above}//End of file bbb.cpp  



Edited by - Advanced Bug on October 18, 2001 8:34:53 AM
Im assuming you are using VC++ when I post this because its all i really ever used but in order to get the functions, simply having it in the same directory as your project isnt enough. You have to include it in your project.

~Vendayan
"Never have a battle of wits with an unarmed man. He will surely attempt to disarm you as well"~Vendayan
At the moment I simply put my functions in the same project as my source code as it doesn''t seem to "find" clsfunc.cpp...

Where are the functions of stdio.h or other .h?
Signed: ___T____
In MSVC, try adding each file to the folders on your Project window.

For the .cpp file, go to the source folder and click add file. Find the file on your computer and add it.

For the .h file, go to the header folder and click add flie. Find the file on your computer and add it.

MSVC will then know where to find your files you indicate with #include in your programs.

MSVC has already defined file paths (and knows where the files are) for the headers and source code for stdio.h, string.h, etc. It knows to search for those based on how it was installed.

I''m guessing you''re new to the MSVC compiler, so I wouldn''t try to play with it just yet.

Hope that helps.

Reuben.

This topic is closed to new replies.

Advertisement