返回"auto"的函数在定义之前不能使用
A function that returns 'auto' cannot be used before it is defined
我有一个用Visual c ++创建的DLL项目和一个CLR项目。 在我的 DLL 项目中,我导出了一个带有"auto"类型的函数。
员工.h
extern "C" STAFFS_API auto GetStaffMap();
如果是工作人员.cpp它有一个 std::map 返回类型。
std::map<int, std::string> staffMap;
auto GetStaffMap()
{
return staffMap;
}
现在在我的 CLR 应用程序中, 我调用此函数:
#include <map>
#include "Staff.h"
std::map<int, std::string> staffMap = Staffs::GetStaffMap();
当我编译程序时,它有一个错误,上面写着:
C3779 'Staffs::GetStaffMap': a function that returns 'auto' cannot be used before it is defined.
更新
我试过了 员工.h
extern "C" STAFFS_API auto GetStaffMap() -> std::map<int, std::string>;
工作人员.cpp
extern "C" auto GetStaffMap() -> std::map<int, std::string> {
return staffMap;
}
但仍然有编译错误:
Error C2526 'GetStaffMap': C linkage function cannot return C++ class 'std::map<int,std::string,std::less<int>,std::allocator<std::pair<const _Kty,_Ty>>>' AmsCppRest c:userslaptop-attendancesourcereposamscpprestamscppreststaff.h
Error C2556 'std::map<int,std::string,std::less<int>,std::allocator<std::pair<const _Kty,_Ty>>> Staffs::GetStaffMap(void)': overloaded function differs only by return type from 'void Staffs::GetStaffMap(void)' AmsCppRest c:userslaptop-attendancesourcereposamscpprestamscppreststaff.cpp
Error C2371 'Staffs::GetStaffMap': redefinition; different basic types
auto
不会延迟找出函数的返回类型。它只是让编译器查看实现,以找出它auto
的。您必须在标头中手动声明返回类型,因为包含标头的代码必须知道返回类型是什么。
你应该声明一个返回的类型,以便编译器知道它。
// Declaration
extern "C" STAFFS_API auto GetStaffMap() -> std::map<int, std::string>;
// Definition
extern "C" auto GetStaffMap() -> std::map<int, std::string>
{
return staffMap;
}
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 返回"auto"的函数在定义之前不能使用
- 在 VS Code 中编码时不能在 C++ 中使用 auto 关键字
- 为什么我不能在递归 lambda 函数中使用 auto
- 我可以在动态知道其类型的模板类对象中使用 auto 关键字吗?我不能在没有初始值设定项的情况下使用 auto 关键字吗?
- 声明适用于 auto,但不能显式声明类型?
- 为什么"auto"在推导返回值时不能接受大括号的初始化列表?
- 为什么 auto 关键字不能与指向函数的指针的初始化列表一起使用?
- 为什么我不能使用带有 auto 的大括号初始化值并将其传递给此函数
- 当不能使用auto和decltype()时,如何避免封闭类类型的重复
- 为什么不能使用 auto 作为模板类型参数(例如 std::array<auto, 5>)?
- 为什么我不能在最新版本的 GCC 中使用 auto 关键字
- Emacs auto-complete-c-headers 不能使用引号
- 为什么我不能将 auto 与 std::thread 一起使用?
- 为什么auto不能用于重载函数
- 为什么"auto&"不能绑定到易失性右值表达式?
- Auto it = vector.begin()结果类型不能转换为const_iterator
- 为什么"auto"不能作为 lambda 参数
- 为什么auto不能和一些lambda一起工作