当我在Visual C ++ 2012中编写这个简单的代码时,我得到了一个有趣的intellisense错误: 类型
的引用"std::vector<Node*, std::allocator<Node*>> &" (non const-qualified)
无法使用类型的值初始化
"std::vector<Node*, std::allocator<Node*>>".
但我们可以成功构建代码而不会出现警告和错误。那么为什么Visual C ++ 2012会报告这样的智能感知错误呢?
#include <vector>
using std::vector;
class Node {
private:
vector<Node*> children;
public:
vector<Node*>& getChildren() {
return children;
}
void addChild(Node* child) {
children.push_back(child);
}
};
void f(Node* root)
{
vector<Node*>& tmp = root->getChildren(); // intellisense error on 'root' here
}
void main()
{
Node root;
Node child1, child2;
root.addChild(&child1);
root.addChild(&child2);
f(&root);
}