0

I want to know that if i can define and declare methods/functions in a .h file and whether i could call them in a test file(.cpp file)or do i need just to declare in the .h file and the deine in a separte .cpp file.

Thanks i am coding in c++

gandhigcpp
  • 587
  • 3
  • 8
  • 16

1 Answers1

2

You can declare and define functions in a header but defining them without making them inline might give you linking errors.The one definition rule is easy to break unless the functions are inline in the latter case.

Unless, You really know what you are doing it is best to declare the functions in a header file and define them in a cpp file.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • :if i have a function as int DateFormat(struct tm* tm) { .... return atoi(date.c_str()); } i can declare in the header file but what arguments to pass in it – gandhigcpp Apr 05 '12 at 10:23
  • You declare it in a header file, include the header file in the cpp file from which you want to *call* the function. Declaring a function, Defining a function body & actually calling a function are three different milestones.You do really need a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Alok Save Apr 05 '12 at 10:34
  • :if in that function defination i have got several variable and if i do according to u as said that defining in cpp file where should i give the declarations f the variable – gandhigcpp Apr 05 '12 at 10:36
  • @gandhigcpp: I already did that. Check the links I have added in the answer as well as the comment. – Alok Save Apr 05 '12 at 10:39