为什么不能在.cpp文件中内联函数或类成员函数定义?例如,在下面的测试示例中,如果我尝试在cpp文件中内联函数或类成员定义,链接器会给我一个未定义引用错误:
test.h
void print();
class A
{
public:
void print() const;
};
test.cpp
#include <test.h>
inline void print()
{
// implementation
}
inline void A::print() const
{
// implementation
}
main.cpp
#include <test.h>
int main(int argc, char** argv)
{
print(); // undefined reference to `print()'
A a;
a.print(); // undefined reference to `A::print() const'
return 0;
}
我已经阅读了这里的一些答案,但我仍然不确定它是如何工作的。
inline
,它们将正常工作甚至可能被内联。 - Bo Persson