1

这个问题是相当理论上的,尽管有趣的是让 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));
4

1 回答 1

6

声明它没有()

maidsafe::dht::PrivateKeyPtr pk;

不幸的是,对于为您提供未初始化值的原始类型,但在 C++11 中,您可以使用以下值进行初始化{}

maidsafe::dht::PrivateKeyPtr pk{};

有关相关的解析问题,请参阅c++ most vexing parse

于 2012-05-09T06:47:46.353 回答