Let's say:
foo.h:
struct foo {
int bar();
};
foo.cpp:
#include "foo.h"
inline int foo::bar() {return 0;}
example.cpp:
#include "foo.h"
#include <iostream>
int main() {
foo lol;
std::cout << lol.bar() << std::endl;
return 0;
}
Compiled with g++ -std=c++20 example.cpp foo.cpp -o example
gives foo::bar()
symbols not found.
In the book C++ Primer 5e at Ch.7.3 p.272 - 273, the authors say that it is possible to declare a class member function first and 'can be made inline later' in the definition. That is:
class Screen {
// ...
Screen &move(pos r, pos c); // can be made inline later
}
inline // we can specify inline on the definition
Screen &Screen::move(pos r, pos c) {
// ...
}
Have I put the inline definition in foo.cpp
in foo.h
and only compile example.cpp
the linker of course does not complain. But what is the problem here?