user123
user123

Reputation: 241

Should I put these functions in a standalone .h file?

I have a project which includes 2 files: main.cc and header1.h. While main.cc instantiates a class "className" and calls the member functions of it, the header file includes definition of the class.

main.cc:

#include <header1.h>
int main(){
    className classobject();
    classobject.function1();
    ...
    }
}

header1.h:

class className{
   public:
   className();
   void function1();
   ...
};

double className::function1(){
    double function2(){
    ...
    }
}

With this, I met an error which says:"a function definition should not be placed before '{' token.

So I decided to put the definition of function2 outside of the class, and put it in a standalone .h file. In that way, my project would include 3 files, namely: main.cc, head1.h and function2.h file:

main.cc:

#include <head1.h>
int main(){
className classobject;
void classobject.function1();
...
}

head1.h:

#include <function2.h>
class className{
double function2();
...
}

function2.h:

double function2(){
...
}

Although function2 can also be defined as the class member function so that it can be moved out of function1 while inside the class, I want to know whether the above mentioned treatment is leagal. Besides, does the creation of header files have some implicit rules established by usage (common treatment)?

Could anyone give some comments? Thanks in advance.

Upvotes: 0

Views: 382

Answers (2)

eerorika
eerorika

Reputation: 238421

The first problem in your code that I see is that you define className::function1 in a header, but forgot to declare it inline. The outcome is that you can only use the header in a single compilation unit . That's a bad design. I recommend defining it in a source file instead.

Secondly, you try to define function2 inside another function. That's not allowed and therefore you got a compilation error. You say that you decided to move it's definition outside the class, but it's definition was already outside the class definition. Moving the definition outside the definition of className::function1 is indeed a correct choice.

If function2 is reusable outside it's use in className::function1, then it's indeed a good idea and perfectly legal to declare it in a separate header. But just like className::function1, you're defining function2 also in the header again and again without declaring it inline and therefore you will get in trouble if you try to use it in multiple compilation units. I recommend defining function2 in a source file as well.

If you include the header in multiple compilation units, then each of those compilation units will contain the definition of the function. The One Definition Rule does not allow defining a function in more than one compilation unit. Functions that are marked inline are treated differently. They may be defined in multiple compilation units as long as they have an identical definition in each.

Upvotes: 0

PinkFloyd
PinkFloyd

Reputation: 2193

Do something like this in your className.hpp

class className{
   public:
        className();
   private:
       void function1();
};

Then in your className.cpp

#include"className.hpp"

className::className(){ /*defintion*/ }

void className::function1(){ /*definition*/ }

Then you could use a makefile in oder to compute the className.o file and link it to the main file.

If you want to define another function (like function2()), you can then define it the className.cpp

Upvotes: 0

Related Questions