将私有数据成员作为默认参数传递给该类的公共方法时出错
Error while passing a private data member as default argument to a public method of that class
我认为标题说明了一切。显示的错误MSVS是
非静态成员引用必须相对于特定对象
我的代码:
struct Node
{
Node(size_t id, int length, int size);
size_t id_;
int length_;
std::vector<Node*> children_;
};
class SuffixTree
{
public:
SuffixTree();
void printSuffixes();
void printSuffixes(Node* current = root_); // ERROR
~SuffixTree();
private:
Node *root_;
};
还有一些类似的方法,我希望用户从main调用这些方法,但由于root_
是私有的,我不得不重载所有这些方法,而用户现在调用重载的方法。这些方法的定义简单如下:
void SuffixTree::printSuffixes()
{
printSuffixes(root_);
}
有什么解决方案吗?
编辑:
void SuffixTree::printSuffixes(Node* current)
{
if (current == nullptr)
return;
if (current->length_ == -1)
std::cout << "leaf" << std::endl;
for (size_t i = 0; i < current->children_.size(); ++i)
printSuffixes(current->children_[i]);
}
默认参数有很多限制。
相反,考虑使用nullptr
作为默认值:
void SuffixTree::printSuffixes(Node* current = nullptr)
{
if (current == nullptr)
current = root_;
// ...
}
相关文章:
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- 将私有数据成员作为默认参数传递给该类的公共方法时出错
- 方法的参数列表出错
- 从同一类中的另一个方法调用方法时出错
- r-使用Rcpp模块公开带有引用参数的c++类方法时出错
- 链接webrtc本机时出错,因为未定义对具有std::string的方法的引用
- 使用字符串调用方法时出错:类型为'size_t和
- Inventor.h中矢量的添加方法出错
- C++ 中头文件中的 Add 方法出错
- 我正在尝试创建一个方法,该方法使用指针算术返回一对,它是否会出错
- 从c++调用非静态java方法时出错
- 调用简单getter方法后出错:basic_string::_S_create
- C++ 当我将父方法作为派生对象调用时出错
- 添加到方法的 C++ 自定义异常会出错
- g++4.8.2上列表方法参数默认初始化时出错
- 设置方法C++时出错
- 在C++11中存储对方法的引用时出错
- c++私有属性可以在构造函数中使用,但seg在其他方法中会出错
- 与Orb匹配的模板:batchDistance方法出错
- 通过包装对象上的函数指针调用类方法出错