本文介绍了C ++变量声明被视为函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题是理论上的,尽管很有趣,是什么使MS VS2010将以下变量声明(在 main
内部)视为函数声明:
The question is fairly theoretical, though it's interesting what makes MS VS2010 treat the following variable declaration (inside main
) like a function declaration:
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
以下几行就像一个符咒一样:
While the following lines work like a charm:
maidsafe::dht::PrivateKeyPtr pk = maidsafe::dht::PrivateKeyPtr();
kNode->node()->Store(key, value, "", ttl, pk, std::bind(&StoreCallback, args::_1, key, ttl));
推荐答案
不使用()
声明它:
maidsafe::dht::PrivateKeyPtr pk;
不幸的是,对于为您提供未初始化值的原始类型,但是在C ++ 11中,您可以使用 {}
进行值初始化:
Unfortunately, for primitive types that gives you an uninitialized value, but in C++11 you can value initialize with {}
:
maidsafe::dht::PrivateKeyPtr pk{};
有关相关的解析问题,请参见 c ++最令人讨厌的解析.
For a related parsing issue, see the c++ most vexing parse.
这篇关于C ++变量声明被视为函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!