![](/img/trans.png)
[英]How do I properly declare a function returning own-defined type, both in the header and the .cpp file?
[英]How can I use an alias thats defined in a header file, in the return type (signature) of a function in the .cpp file?
我有一个 class, Tracker
,我在其中声明了一个别名
从Tracker.h
:
class Tracker {
...
using ArgsMap = std::unordered_map<std::string, std::string>;
std::shared_ptr<ArgsMap> getArgsMapForTask(std::string task);
...
}
在 .cpp 文件中,我定义了 function:
#include Tracker.h
...
// ArgsMap here gives error: Use of undeclared identifier 'ArgsMap'
std::shared_ptr<ArgsMap> Tracker::getArgsMapForTask(std::string taskName)
{
ArgsMap a; // this gives no error, compiler recognizes ArgsMap
}
如何在 function 签名中使用ArgsMap
?
问题不在于别名在 header 文件中,这完全不相关。
问题是别名是在 class 的 scope 中定义的,所以如果你想在 class 之外使用它需要限定它: Tracker::ArgsMap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.