C++ 函数声明中有什么错误?
请向我解释一下,这种方法的声明/描述中有什么错误C++ 函数声明中有什么错误?,c++,visual-studio-2010,C++,Visual Studio 2010,请向我解释一下,这种方法的声明/描述中有什么错误 class Set { struct Node { // ... }; // ... Node* &_getLink(const Node *const&, int) const; // ... }; Node* &Set::_getLink(const Node *const &root, int t) const { // ... } 我没有看
class Set
{
struct Node {
// ...
};
// ...
Node* &_getLink(const Node *const&, int) const;
// ...
};
Node* &Set::_getLink(const Node *const &root, int t) const
{
// ...
}
我没有看到错误,但是编译器(MS VS C++)给出了许多语法错误。您忘记了完全限定
节点的名称(该名称在集合
的范围内定义):
如果没有完全限定,编译器将查找名为节点的全局类型,该类型不存在。问题是范围问题。您需要在此处添加前缀节点
:
Set::Node* &Set::_getLink(const Node *const &root, int t) const
{
// ...
}
实际上,节点
在遇到时是未知的(您在名称空间范围内,而不是在集合
的范围内)。您还可以使用auto
:
auto Set::_getLink(const Node *const &root, int t) const -> Node *&
{
// ...
}
在->
之后,您处于集合的范围,并且节点
是已知的。您不在全局范围内定义节点
所以使用这个代码
//by Set::Node we give compiler that this function exist in class Node
Set::Node* &Set::_getLink(const Node *const &root, int t) const
{
// ...
}
尝试将::Node设置为您的代码编译器错误是什么?把它放在题目里。
//by Set::Node we give compiler that this function exist in class Node
Set::Node* &Set::_getLink(const Node *const &root, int t) const
{
// ...
}