c++ - 错误: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);
}
解决方案
如评论中所述,将 mSource (uint8_t) 转换为 NetworkConnection (Class) 将失败。正如你所说 -> // the source being a NetworkConnection is a guess right now
。
您可能应该在代码的另一部分进行强制转换。如果提供更多代码,那么我们可以提供帮助。
/安德斯
推荐阅读
- linux - 为什么 `stack --version` 似乎显示的版本与安装的版本不同?
- angular - 这实际上是循环依赖还是误报?
- virtual-machine - 从虚拟机访问共享文件夹中的符号链接/软链接
- string - 在powershell的全名文本字段中使用姓氏填充变量
- linux - 无法为特定内核版本构建 Linux 内核模块
- filter - 根据tableau中的相对日期范围过滤器计算总天数
- node.js - UserNotFound:找不到用户 nodejs@sDatabase
- java - 如何在ANTLR中分离出块的开始和结束标签
- spring-boot - 根据 Spring profile 属性设置字段
- exception - 如何在 CodenameOne 中获取设备上的堆栈跟踪信息?