【发布时间】:2023-03-16 00:02:01
【问题描述】:
所以我尝试对 Reader 文件使用自动函数,当我在自己的 .cpp 文件和 .h 文件中声明它时,我收到错误:
'Reader':返回'auto'的函数在定义之前不能使用
但该函数在声明 main 函数的 .cpp 文件中完美运行。
Reader.cpp
auto Reader(std::string Location, int Value)
{
//Code - I removed the code for simplicity sake
return 1;
}
阅读器.h
auto Reader(std::string Location, int Value);
这是它在主函数中的调用方式:
int Renderer = Reader("Engine.Setup", Test);
【问题讨论】:
-
在
main中,编译器应该有权访问的是标头中的auto Reader(std::string Location, int Value);声明,它没有提供关于Reader返回的有用信息。您可能需要一个尾随返回类型。或者直接返回int -
这就是为什么我评论了 //Code 我的代码在哪里,但为了简单起见,我把 return 1;
-
我想你没抓住我的意思。 Reader.cpp 知道
Reader返回什么。 Reader.h 没有,所以main也不会知道,除非您包含 Reader.cpp 以获得完整的定义。但是包含一个 cpp 文件是一种非常恶劣的行为,以至于您的姓氏将被必须处理后果的程序员永远诅咒。 -
此时我不会使用 auto
-
@user12826193 在正确使用
auto的情况下,一般来说没有任何问题。但是这个例子不是auto的正确使用。