c++ - 将类声明与定义分开
问题描述
我想将类定义和声明分成 2 个单独的文件:foo.hpp
和foo.inl
.
foo.hpp
文件具有Foo
带有其描述的类声明,并且该文件还包括foo.inl
:
/* foo.hpp */
// Foo class description comment
class Foo;
#include "foo.inl"
foo.inl
包含Foo
没有任何代码描述注释的定义。
/* foo.inl */
class Foo {
Foo() = default;
void bar() {
/* do something */
}
}
我正在尝试为Foo
's 的方法编写评论,foo.hpp
使其看起来像这样:
/* foo.hpp */
// Foo class description comment
class Foo;
// This is my default constructor
Foo::Foo();
// This is my very helpful function
Foo::bar();
#include "foo.inl"
但是编译器给出了一个可以理解的错误:invalid use of incomplete type 'class Foo'
.
那么有什么方法可以声明函数并以这种方式为它们写注释吗?
解决方案
如果要拆分类方法的定义和声明,则必须定义类:
// .h
// That is my class Foo
class Foo {
// Constructor
Foo();
// This is my very helpful function
void bar();
};
和
// cpp
Foo::Foo() = default;
void Foo::bar() {
/* do something */
}
推荐阅读
- c# - C#和Ruby之间的AES加密和解密
- python - python re.compile match在windows的完整路径中不匹配反斜杠
- python - 与 PORT 一起检查和打印 IP
- java - Spring Scheduler/Executor的AOP特性
- javascript - Node js异步等待函数不会互相等待
- linux - 缩放调节器配置在机器上不可用
- node.js - 通过 prisma 更新关系数据
- java - Java,具有比较器或可比较的泛型类
- airflow - Airflow 中的“所有者”属性是做什么用的?
- python - Mypy 不匹配 aiohttp 的 Response 和 json_response