首页 > 解决方案 > 错误:any_cast(any&&) 在 Windows 上损坏但在 Linux 中有效

问题描述

首先,我是 C++ 新手,所以要温柔。在我的跨平台项目中,我遇到了一个问题,我无法通过 Maven 中的 nar 构建器使用 VS Studio 2019 进行整理。该构建在 Linux 上运行正常,但在 Windows 10 x64 上构建失败:

[ERROR] OUTPUT>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\any(429): error C2338: any_cast<T>(any&&) requires T to be constructible from remove_cv_t<remove_reference_t<T>>
[INFO] OUTPUT>c++/SRTServer.cpp(188): note: see reference to function template instantiation '_Ty &std::any_cast<std::shared_ptr<NetworkConnection>&>(std::any &&)' being compiled
[INFO] OUTPUT>        with
[INFO] OUTPUT>        [
[INFO] OUTPUT>            _Ty=std::shared_ptr<NetworkConnection> &
[INFO] OUTPUT>        ]

NetworkConnection 在包含的 h 文件中,包括

class NetworkConnection {
public:
    NetworkConnection();
    virtual ~NetworkConnection();
    std::any object;
    uint16_t connectorId;
};

SRTServer.cpp 的第 183-193 行

void gotData(ElasticFrameProtocol::pFramePtr &rPacket) {
    std::cout << "Got NAL-units of size " << unsigned(rPacket->mFrameSize) <<
                " pts " << unsigned(rPacket->mPts) << " is broken? " << rPacket->mBroken << 
                " from EFP connection " << unsigned(rPacket->mSource) << std::endl;
    // TODO the NetworkConnection must be looked up to get the server id and client id
    auto nc = std::any_cast<std::shared_ptr<NetworkConnection> &>(rPacket->mSource); // the source being a NetworkConnection is a guess right now
    // get the client from the network connection
    auto client = std::any_cast<std::shared_ptr<Client> &>(nc->object);
    // call the receive method
    recvData(rPacket->pFrameData, rPacket->mFrameSize, client->serverId, client->connectorId, rPacket->mStream);
}

标签: c++mavenvisual-c++

解决方案


如评论中所述,将 mSource (uint8_t) 转换为 NetworkConnection (Class) 将失败。正如你所说 -> // the source being a NetworkConnection is a guess right now

您可能应该在代码的另一部分进行强制转换。如果提供更多代码,那么我们可以提供帮助。

/安德斯


推荐阅读