std ::每当在CPP文件中实现标头内的功能时,任何不良演员都会返回

发布于 2025-01-19 15:46:13 字数 815 浏览 4 评论 0原文

主要


#include <iostream>
#include <unordered_map>
#include <string>
#include "myclass.h"

int main(int argc, const char * argv[]) {
    any a = myclass::returnthis();
    unordered_map<string, any>* hmap = any_cast<unordered_map<string, any>*>(a);
    return 0;
}


这是

#ifndef myclass_h
#define myclass_h

#include <any>

using namespace std;

class myclass {
public:
    static any returnthis();
};

#endif /* myclass_h */

#include "myclass.h"
#include <any>
#include <unordered_map>
#include <string>

any myclass::returnthis() {
    return new unordered_map<string, any>();
}

我的 我正在研究MacOS,并且尝试过Xcode和Clion。 但是,如果我将功能的实现放置在标题文件中,则问题将消失。为什么?

This is my main.cpp


#include <iostream>
#include <unordered_map>
#include <string>
#include "myclass.h"

int main(int argc, const char * argv[]) {
    any a = myclass::returnthis();
    unordered_map<string, any>* hmap = any_cast<unordered_map<string, any>*>(a);
    return 0;
}


this is myclass.h

#ifndef myclass_h
#define myclass_h

#include <any>

using namespace std;

class myclass {
public:
    static any returnthis();
};

#endif /* myclass_h */

and this is myclass.cpp

#include "myclass.h"
#include <any>
#include <unordered_map>
#include <string>

any myclass::returnthis() {
    return new unordered_map<string, any>();
}

Now, any_cast will report bad any cast. I am working on macOS and I have tried Xcode and CLion.
However, if I put the implementation of the function inside the header file, the problem vanishes. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文