日
#ifndef __T_H__
#define __T_H__
class A
{
int j;
public:
A(int i);
};
#endif
Run Code Online (Sandbox Code Playgroud)
t.cpp
#include "t.h"
inline A::A(int i):j(i){}
Run Code Online (Sandbox Code Playgroud)
主程序
#include "t.h"
int main(void)
{
A a(2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:
$ g++ t.cpp main.cpp -o main
/tmp/ccRjri7I.o: In function `main':
main.cpp:(.text+0x15): undefined reference to `A::A(int)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
如果我从实现中删除内联,就可以了。不知道为什么会这样。
内联必须在使用它们的同一翻译单元中定义。通过在 .cpp 文件中定义“内联”函数,它只能在同一个 .cpp 文件中使用。您需要将其移动到头文件或某些特殊的“内联”文件中,某些项目更喜欢将它们的实现细节隐藏起来(然后您#include将该内联文件,无论是在您的头文件中还是在 main.cpp 中) .
| 归档时间: |
|
| 查看次数: |
1233 次 |
| 最近记录: |