【发布时间】:2019-12-27 13:17:17
【问题描述】:
我想以这样的方式定义一个自动返回类型的函数,如果我包含标题,我可以从多个 .cpp 文件中调用它。我有 4 个文件
head.hpp - 函数在哪里
#ifndef HEAD_HPP
#define HEAD_HPP
auto f();
#endif
head.cpp - 函数声明的地方
#include "head.hpp"
auto f(){
return [](){
return 10;
};
}
test1.cpp - 在哪里使用
#include "head.hpp"
int foo(){
auto func = f();
return f();
}
main.cpp - 也用在哪里
#include "head.hpp"
int foo();
int main(){
auto fu = f();
return fu() + 5 + foo();
}
所有文件一起编译 我仍然收到错误:
错误:在扣除“auto”之前使用“auto f()”
自动 fu = f();
【问题讨论】:
-
记住ODR(一个定义规则)。
标签: c++ lambda header auto include-guards