I have a problem, may be dumb.
The thing is, I am unable to inline a constructor in a class.
Consider I have a class called Foo.
If I write the implementation of Foo something like this:
class Foo {
int p;
public:
Foo() { p = 1; }
};
or even like this:
class Foo {
int p;
public:
Foo();
};
inline Foo::Foo() {
p = 1;
}
The program won’t compile.
I use the class using the standard method:
Foo obj;
Now when I run g++ main.cpp foo.cpp, I get:
/tmp/ccyVtxvp.o: In function `main':
main.cpp:(.text+0x17): undefined reference to `Foo::Foo()'
collect2: ld returned 1 exit status
At the same time, when I use the same code, after compiling the class as a shared library (with factory functions to return object pointer), it works properly.
Any guesses why this is happening?
Inline functions must be defined in every source file in which they are used. The easiest way to achieve this is to put the definition(body) of the function in the header file and include the header wherever the function is used