问题是相当理论化的,尽管有趣的是MS VS2010如何处理以下变量声明(在main
内),如函数声明:
typedef std::shared_ptr<asymm::PrivateKey> PrivateKeyPtr;
...
void main()
{
...
maidsafe::dht::PrivateKeyPtr pk(); // I'm trying to init variable here, though it thinks it's function declaration
kNode->node()->Store(key, value, "", ttl, pk, std::bind(&StoreCallback, args::_1, key, ttl));
}
抛出以下异常:
Error 5 error C2664: 'maidsafe::dht::Node::Store' : cannot convert parameter 5 from 'maidsafe::dht::PrivateKeyPtr (__cdecl *)(void)' to 'maidsafe::dht::PrivateKeyPtr' C:\Projects\MaidSafe-DHT\src\maidsafe\dht\demo\demo_main.cc 286 1 KademliaDemo
虽然以下几行像魅力一样:
maidsafe::dht::PrivateKeyPtr pk = maidsafe::dht::PrivateKeyPtr();
kNode->node()->Store(key, value, "", ttl, pk, std::bind(&StoreCallback, args::_1, key, ttl));
答案 0 :(得分:6)
在没有()
:
maidsafe::dht::PrivateKeyPtr pk;
不幸的是,对于提供未初始化值的原始类型,但在C ++ 11中,您可以使用{}
初始化值:
maidsafe::dht::PrivateKeyPtr pk{};
有关相关的解析问题,请参阅c++ most vexing parse。