I find it a very appealing programming style define all the methods of a struct/class directly inline in the struct, eg only define structs this way:
"A.hpp"
class A;
"A.cpp"
class A
{
A() = default;
void method() { }
};
rather than
"A.hpp"
class A
{
A();
void method();
};
"A.cpp"
A::A() = default;
void A::method() { }
This means putting classes/struct definitions in a code file and only including declaration in header files.
However, this runs into the problems when the compiler would need to know the struct/class layout in other code files.
Is there a programming style that supports this? Perhaps only getting and setting fields through getters/setters? This way would however lead to large amounts of boilerplate.
Just including the struct/class definition in the header file leads to issues with mutually recursive header files, unless one explicitly puts the declarations in another.
I can achieve this programming style in a language like D with a module system, but how does one achieve it in C++?
What are you doing is called forward declaration.
You will be not able to do anything (f.e. call methods, create instances) with this class in all other translation units, except one .cpp file, where you define it. So, this idea is completely useless and bad. And you will get a boatload of linker errors.
So, you need to either stick to style #2. Or define inline methods in header file, like this
struct A {
A() {} // something
int func() { /* function body */
}
Related
Disclaimer: This is probably a basic question, but I'm a theoretical physicist by training trying to learn to code properly, so please bear with me.
Let's say that I want to model a fairly involved physical system. In my understanding, one way of modelling this system is to introduce it as a class. However, since the system involved, the class will be large, with potentially many data members, member functions and subclasses. Having the main program and this class in one file will be very cluttered, so to give a better overview of the project I tend to put the class in a separate .h file. Such that I'd have something like:
//main.cpp
#include "tmp.h"
int main()
{
myclass aclass;
aclass.myfunction();
return 0;
}
and
// tmp.h
class myclass
{
// data members
double foo;
double bar;
public:
// function members
double myfunction();
};
double myclass::myfunction()
{
return foo + bar;
}
This however, amounts to the following compiler warning in my new compiler: function definitions in header files can lead to ODR violations. My question then is this: what is actually the preferred way of dealing with a situation like this? I guess I can just make tmp.h into tmp.cpp, but to the best of my understanding, this is the intended use of .h files?
Normally, a class definition goes in an ".h" file and its member functions' definitions go in a ".cpp" file.
If you want to define member functions in the header, you need to either declare them inline, or write them inside the class definition (which makes them implicitly inline).
Adding to the other answers:
This is a function definition:
double myfunction()
{
return foo + bar;
}
This is a function declaration:
double myfunction();
The purpose of the declaration is to declare the unique signature of the function to other code. The function can be declared many times, but can only have one definition, hence the ODR (One Definition Rule).
As a basic rule to start with, put function declarations in header files, and put definitions in source files.
Unfortunately in C++, things rapidly get more complicated.
The problem with just having the function signature available is that you can't easily optimise the code in the function, because you can't see it. To solve that problem, C++ allows function definitions to be put in headers in several circumstances.
You'll have to either use the inline keyword or put the definition of myclass in a .cpp file.
myclass.hpp
#ifndef MY_CLASS_H
#define MY_CLASS_H
class myclass
{
public:
double myfunction( );
private:
double foo;
double bar;
};
#endif
myclass.cpp
#include "myclass.hpp"
double myclass::myFunction( )
{
return foo + bar;
}
Or you can define the function in the header (myclass.hpp) using inline.
#ifndef MY_CLASS_H
#define MY_CLASS_H
class myclass
{
public:
double myfunction( );
private:
double foo;
double bar;
};
inline double myclass::myFunction( )
{
return bar + foo;
}
#endif
If you define the myFunction function in the class declaration then you can omit the use of the inline keyword.
#ifndef MY_CLASS_H
#define MY_CLASS_H
class myclass
{
public:
double myfunction( )
{
return foo + bar;
}
private:
double foo;
double bar;
};
#endif
ODR stands for One Definition Rule. It means that everything should have one and only one definition. Now, if you define a function in a header file, every translation unit that includes that header file will get a definition. That obviously violates the ODR. That's what the compiler is warning about. You have couple of ways to work around that:
Move the function declaration to a cpp file. That way there is a single definition.
Make the function inline. This means that there may be multiple definitions of this function, but you're sure that all are identical and the linker may use one those and ignore the rest.
Make the function static (doesn't apply to class-methods). When a function is static each translation unit gets it's own copy of the method, but they are all different functions and belong to one and only one compilation unit. So it's OK.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm new in c++.
I'm required to define member functions. I am more familiar with java. I actually confused by the term "member function".
Do we have to define it in the header .h file or the .cpp file?
I am confused by the term of member function
Everything that belongs to a class or a struct is called a member of that class. Hence, we can have member functions. Below is a little exmaple:
class MyClass
{
public:
MyClass();
void myFunction();
int number;
}
Here you have myFucntion() and number members of MyClass
Do we have to define it in the header .h file or the .cpp file?
In C++, it doesn't matter where you define or declare a function or a class.
But keep in mind that putting declaring classes in .h files is better. It will lead to faster compilations of your project. So you should have something like:
// myClass.h
class MyClass
{
public:
MyClass();
void myFunction();
int number;
}
// myClass.cpp
myClass::myClass()
{
// this is a constructor
number = 10;
}
void myClass::myFunction()
{
// this is my function
cout << number;
}
Every function in C++ is a member
Member of a namespace. These are sometimes called "global functions", although that termis not completely correct. They consist of all functions in the global namespace and any functions in user defined namespaces, hence they are called namespace-scope functions.
Member of a class. These are called member functions (even though they are not the only functions that are members!). They can be further separated by non-static member functions and static member functions (I take it you know the difference of this from Java).
Alternate syntax:
class MyClass{
public:
void MyFunction(void); // prototype
}
MyClass::MyFunction(void){ // actual function
// code goes here
}
Think of member function as a method.
In Java, your method (member function) within a class might look something like:
public class MyClass {
public void MyMethod() {
// do stuff
}
}
This is very much the same in C++:
class MyClass
{
public:
void MyMethod();
}
As far as how I've been taught, you declare your class and its members in a header file (.h) and then define them in a .cpp file. You don't have to do it that way and can keep everything with your .cpp file. It's just a better practice to separate them.
in C++ does not matter ...
member functions are just functions inside class (or struct)
you can have headers
you can also have body only ...
if you separate headers and bodies into *.h(pp) and *.cpp files
then you can cross referencing more classes to each other
here some examples:
class C1
{
public:
int a,b,c; // some variables
void f0(); // header only
void f1() { c=a+b; }; // inline body only
friend void f2(); // not a member but can have access to class private things
friend void f3(C1 &x); // not a member but can have access to class private things
};
void C1::f0() { c=a+b; } // body(C1 member) ... can be in separate file
void f2() { C1 c1; c1.c=c1.a+c1.b; } // just function not a member
void f3(C1 &x) { x.c=x.a+x.b; } // just function not a member
if your not member function are accesing only public parts of class then they do not need to be friend.
also there are some restrictions for inline bodies on some compilers
like you can not use asm {} for example...
First, your question "Do we have to define it in the header .h file or the .cpp file?"
You can define (write code for) member functions in either, with different implications.
Any function defined in a header file needs to be an inline function (because it will be included to many .cpp files, and being inline means, this is ok). Luckily, if you combine declaration and definition and do it inside class {...}, they are automatically considered inline, so it "just works".
If you define the member function in a .cpp file, then you need to have the declaration inside class {...}. And if you want to use the class from many .cpp files, then it should go to a .h file, but it can also be earlier in the same .cpp file.
More explanation:
Member function is same as method. Also, for the purposes of this answer, function overloads (same name but different arguments, return value is not considered) are different functions.
In C++ there is separate declaration and definition, though they can be rolled into one. Any function is declared by having just ; instead of function body { /* ...code... */ }. If this declaration is inside a class {...} or struct {...}, then it's a member function. A function call is ok in code as long as there's declaration before the call. Then later on linker will complain if there's no actual function definition, that is a block of code to call. There can be any number of declarations for a function, but only one definition (except for inline functions, where compiler and linker assume duplicate definitions are all the same, so they better be or funny things can happen).
About .h and .cpp files: C++ (and C), .h files are eseentially copy-pasted to the .cpp file during compilation. You could remove the #include and copy paste the contents, and it would be exactly same thing for compiler. But from practical standpoint, definitions go to a .h file, and then they are included in may .cpp files, and because they are declarations, it's ok to have them as many times as you like. But actual definitions go to .cpp files, so they get compiled exactly once, so linker is happy.
Finally a bit about inline functions. If you have a member function definition inside class {...}, it is automatically considered to be inline. You include the same .h file with these inline definitions to many .cpp files, but it is ok because they are by default marked inline (and also, since the code is in same .h file, all definitions should be the same, unless you use some #ifdef preprocessor stuff wrong).
Example:
myclass.h
class MyClass {
void memberFunc1() {
// here is function declaration and definition combined,
// by default inline, so can be include to many .cpp
}
void memberFunc2(); // here's just declaration
}
myclass.cpp
MyClass::memberFunc2() {
// defintion of member function, not inline, must exist exactly once
}
I am working on a codebase that is not my own, that has the following layout:
object.h:
// Objects are defined
// #include "tickets.h" (functions to access the objects)
// An access-handler object is defined
I want to introduce a class that knows about the objects, can be accessed from functions in tickets.h, but can also use the access-handler object. The functions are separate, i.e. class functions that are called in tickets.h do not use the access-handler (I wouldn't know where to start if that weren't the case).
Therefore my class needs to be defined before tickets.h, but some of its functions need to be defined after the access-handler. Is there a way to do this without splitting it up into two header files something like the following:
// Objects are defined
// -- include declaration of class, and definition of functions that tickets.h needs
// #include "tickets.h"
// An access-handler object is defined
// -- include functions of class that need the access-handler
This seems very messy splitting things up like this into two separate files, I was hoping to keep everything contained.
Thanks for any help, I clearly only have a very rudimentary understanding of declarations/definitions in c++.
EDIT: If I use forward declaration and include it before tickets.h (with the class declared in mynewclass.h and functions defined in mynewclass.cc) will mynewclass.cc be able to use objects declared after the inclusion of mynewclass.h? Namely the access-handler object.
EDIT2: Something like this:
object.h:
class obj { // definition }
#include "tickets.h"
class obj_handler {
public void handle { // code }
}
tickets.h:
void do_something(obj o){
communicator.foo();
}
My object (communicator):
class communicator {
public:
void foo() { // code }
void bar() { // use handle() from obj_handler }
}
As you can see, my communicator needs to be used in tickets.h, but can't be defined until after obj_handler. So where should I include it?
If I correctly understand your question - you can use forward declaration to solve this problem. This will allow you to declare some class before defining it's methods. For example:
// this is forward declaration of class A, no definition provided
class A;
class B
{
// uses A
A * a_;
};
// class A definition
class A
{
// may use class B now
B * b_;
};
I'm not quite sure whether I understand this right and don't have enough reputation here yet to make this a comment, so let me try to answer your question this way, please feel free to follow up if I'm guessing wrong:
I believe what you are referring to is an entire class definition, i.e., one including all function definitions within the class declaration. Other than that, it is not very common to see object definitions followed by preprocessor directives. What is typical though is a forward declaration of functions and a class prototype.
So, for example, you could declare in some header.h:
class C
{
public:
void method1(void);
int method2(void);
};
And in some implementation.cpp the definition of the functions like:
void C::method1(void) { /*...*/ }
In the other file preceded in the inclusion chain by your access-handler you then define the other function:
int C::method2(void) { /*...*/ }
What do you mean by access-handler, by the way?
Oh, and your linker likely will yell somewhat at you if you do function definition in a header file.
With regard to your addenda: everywhere you put a forward declaration, loosely speaking, the compiler will insert a copy of the declaration in question, consider it a soft link in the context of file systems. There are negative implications associated with it, like increased duration and the memory load of compilation if you have many forward declarations of the function signature or class. It's impossible to tell whether this will word in your particular situation since only you know the actual code in question. But most likely it would work.
Take a look at these pages:
http://en.wikipedia.org/wiki/Forward_declaration
When can I use a forward declaration?
I was wondering if there is a way to put objects of a class in a header?
For example:
class MyClass {
} object;
If I made an object there and then include the header in more than one source file, I get an error of multiply defined symbols found. I am new so don't understand fully everything.
Also, I want to do something like object.function() inside of a different function which is a member of a different class how can I do that?
Assuming you want a single object, in the header file just declare the object:
extern blabla object;
and then in one source file define it:
blabla object;
As for calling a method on an object from a different class, that is perfectly fine as long as the method is public.
class foo
{
public:
void public_method();
private:
void private_method();
};
void some_other_class::method(foo &f)
{
f.public_method(); // this is fine
f.private_method(); // this is not okay, private_method can only be called from
// within foo
}
There's also a third visibility (protected), which comes into play once you start dealing with inheritance.
You can find ways to do it (see the other answers), but just don't do it. As you said, you are new to C++, so better learn the good practices, and when you need global objects - create them in the source (cpp) file.
Besides it, try to avoid using global objects at all, and define your objects inside the classes or functions.
It's not a good practice to put definitions in header files as this would result to the error you encountered. Although there are ways to get around this (extern, header guard), it should be avoided as much as possible. Just remember, put declarations in header files, definitions in source files.
About your second question note you can also call a static method using the :: operator and the class name (without an instance) :
void AnOtherClass::method()
{
TheFirstClass::static_method();
}
You could also make a use of the singleton pattern, if that fits your need in this case.
For your second question. You can always make a class object as a (private) member variable of the class you want to call object.function() from.
For example:
// File a.h, includeguards etc. left out for clarity
class A {
public:
void func();
};
// File b.h
#include "a.h"
class B {
public:
void func();
private:
A object;
};
// File b.c
#include "b.h"
void B::func()
{
object.func();
}
Hope that helps.
I'm trying to create a C++ class, with a templated superclass. The idea being, I can easily create lots of similar subclasses from a number of superclasses which have similar characteristics.
I have distilled the problematic code as follows:
template_test.h:
template<class BaseClass>
class Templated : public BaseClass
{
public:
Templated(int a);
virtual int Foo();
};
class Base
{
protected:
Base(int a);
public:
virtual int Foo() = 0;
protected:
int b;
};
template_test.cpp:
#include "template_test.h"
Base::Base(int a)
: b(a+1)
{
}
template<class BaseClass>
Templated<BaseClass>::Templated(int a)
: BaseClass(a)
{
}
template<class BaseClass>
int Templated<BaseClass>::Foo()
{
return this->b;
}
main.cpp:
#include "template_test.h"
int main()
{
Templated<Base> test(1);
return test.Foo();
}
When I build the code, I get linker errors, saying that the symbols Templated<Base>::Templated(int) and Templated<Base>::Foo() cannot be found.
A quick Google suggests that adding the following to main.cpp will solve the problem:
template<> Templated<Base>::Templated(int a);
template<> int Templated<Base>::Foo();
But this does not solve the problem. Adding the lines to main.cpp does not work either. (Though, interestingly, adding them to both gives 'multiply defined symbol' errors from the linker, so they must be doing something...)
However, putting all the code in one source file does solve the problem. While this would be ok for the noddy example above, the real application I'm looking at would become unmanageable very fast if I was forced to put the whole lot in one cpp file.
Does anyone know if what I'm doing is even possible? (How) can I solve my linker errors?
I would assume that I could make all the methods in class Templated inline and this would work, but this doesn't seem ideal either.
With templated classes, the definitions must be available for each translation unit that uses it. The definitions can go in a separate file, usually with .inl or .tcc extension; the header file #includes that file at the bottom. Thus, even though it's in a separate file, it's still #included for each translation unit; it cannot be standalone.
So, for your example, rename template_test.cpp to template_test.inl (or template_test.tcc, or whatever), then have #include "template_test.inl" (or whatever) at the bottom of template_test.h, just before the #endif of the include guard.
Hope this helps!
The problem is that when your Templated file is compiled, the compiler doesn't know what types it will need to generate code for, so it doesn't.
Then when you link, main.cpp says it needs those functions, but they were never compiled into object files, so the linker can't find them.
The other answers show ways to solve this problem in a portable way, in essence putting the definitions of the templated member functions in a place that is visible from where you instantiate instances of that class -- either through explicit instantiation, or putting the implementations in a file that is #included from main.cpp.
You may also want to read your compiler's documentation to see how they recommends setting things up. I know the IBM XLC compiler has some different settings and options for how to set these up.
The C++ FAQ-lite covers this, and a couple of ways round it.
You don't have to make all the methods "inline", but you should define the method bodies in template_test.h, rather in template_test.cpp.
Some compilers can handle this split, but you have to remember that at one level, templates are like macros. for the compiler to generate the a template for your particular , it needs to have the template source handy.
When the compiler is compiling main.cpp, it sees the class definition has member function declarations, but no member function defintions. It just assumes that there must be a definition of "Templated" constructor and Foo implementation somewhere, so it defers to the linker to find it at link time.
The solution to your problem is to put the implementation of Templated into template.h.
eg
template<class BaseClass>
class Templated : public BaseClass
{
public:
Templated(int a) : BaseClass(a) {}
virtual int Foo() { return BaseClass::b; }
};
Interestingly, I could get your code to link by putting this at the end of template_test.cpp.
void Nobody_Ever_Calls_This()
{
Templated<Base> dummy(1);
}
Now the compiler can find an instance of Templated to link with. I wouldn't recommend this as a technique. Some other file might want to create a
Templated<Widget>
and then you'd have to add another explicit instantiation to template_test.cpp.