带指针的 C++ 默认参数
C++ Default Argument with Pointers
我正在编写一个用 C++ 实现后缀特里树的程序。我正在尝试声明一个没有参数的递归函数,但它需要传递一个指向自身的指针。
我是这样定义的
public:
string longestRepeat(Node*);
在头文件中,并且
string Trie::longestRepeat(Node* start = &nodes[0]){
string deepest = "";
for(unsigned int i = 0; i < start->getEdges(); i++){
string child_deepest = longestRepeat(start->getChild(i));
if(child_deepest.length() > deepest.length())
deepest = child_deepest;
}
return deepest;
}
在 .cpp 文件中,其中节点是先前声明的数据结构。
然而,在主函数中简单地调用 trie.longestRepeat()
会导致错误 "no matching function call for Trie::longestRepeat()
. Candidate expects 1 argument, 0 provided"。
对于成员函数,default argument可以声明在out-of-class定义上,但是使用默认参数调用成员函数只能在可以看到定义的翻译单元中进行.
这意味着您可以将 Trie::longestRepeat
的定义移动到头文件中以修复错误。
或者让事情更简单,在声明而不是定义中声明默认参数。例如
// header
public:
string longestRepeat(Node* start = &nodes[0]);
// implementation
string Trie::longestRepeat(Node* start) {
...
}
For a member function of a non-template class, the default arguments
are allowed on the out-of-class definition, and are combined with the
default arguments provided by the declaration inside the class body.
class C {
void f(int i = 3);
void g(int i, int j = 99);
};
void C::f(int i = 3) { // error: default argument already
} // specified in class scope
void C::g(int i = 88, int j) { // OK: in this translation unit,
} // C::g can be called with no argument
默认参数需要放在声明中(头文件中),如果放在第二个声明中(定义中),它只会被看到第二个声明的调用使用:
struct Trie {
std::string longestRepeat(Node*);
};
int main() {
Trie{}.longestRepeat(); // Error
}
std::string Trie::longestRepeat(Node *p = &nodes[0]) { }
void g() {
Trie{}.longestRepeat(); // Ok
}
但是您可能应该做的是创建 longestRepeat
的 public 版本,它调用 private/protected 版本 &nodes[0]
:
struct Trie {
std::string longestRepeat() { // No arguments
longestRepeat_(&nodes[0]);
}
private:
std::string longestRepeat_(Node *); // Real implementation
};
我正在编写一个用 C++ 实现后缀特里树的程序。我正在尝试声明一个没有参数的递归函数,但它需要传递一个指向自身的指针。
我是这样定义的
public:
string longestRepeat(Node*);
在头文件中,并且
string Trie::longestRepeat(Node* start = &nodes[0]){
string deepest = "";
for(unsigned int i = 0; i < start->getEdges(); i++){
string child_deepest = longestRepeat(start->getChild(i));
if(child_deepest.length() > deepest.length())
deepest = child_deepest;
}
return deepest;
}
在 .cpp 文件中,其中节点是先前声明的数据结构。
然而,在主函数中简单地调用 trie.longestRepeat()
会导致错误 "no matching function call for Trie::longestRepeat()
. Candidate expects 1 argument, 0 provided"。
对于成员函数,default argument可以声明在out-of-class定义上,但是使用默认参数调用成员函数只能在可以看到定义的翻译单元中进行.
这意味着您可以将 Trie::longestRepeat
的定义移动到头文件中以修复错误。
或者让事情更简单,在声明而不是定义中声明默认参数。例如
// header
public:
string longestRepeat(Node* start = &nodes[0]);
// implementation
string Trie::longestRepeat(Node* start) {
...
}
For a member function of a non-template class, the default arguments are allowed on the out-of-class definition, and are combined with the default arguments provided by the declaration inside the class body.
class C { void f(int i = 3); void g(int i, int j = 99); }; void C::f(int i = 3) { // error: default argument already } // specified in class scope void C::g(int i = 88, int j) { // OK: in this translation unit, } // C::g can be called with no argument
默认参数需要放在声明中(头文件中),如果放在第二个声明中(定义中),它只会被看到第二个声明的调用使用:
struct Trie {
std::string longestRepeat(Node*);
};
int main() {
Trie{}.longestRepeat(); // Error
}
std::string Trie::longestRepeat(Node *p = &nodes[0]) { }
void g() {
Trie{}.longestRepeat(); // Ok
}
但是您可能应该做的是创建 longestRepeat
的 public 版本,它调用 private/protected 版本 &nodes[0]
:
struct Trie {
std::string longestRepeat() { // No arguments
longestRepeat_(&nodes[0]);
}
private:
std::string longestRepeat_(Node *); // Real implementation
};