0

I think the title says it all. The error MSVS showing is

a nonstatic member reference must be relative to a specific object

My code:

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_;
};

There are a few more methods similar to these such that I want the user to call these methods from main but since root_ is private, I had to overload all those methods and the user now calls the overloaded methods instead. The definition of these methods are simply like:

void SuffixTree::printSuffixes()
{
    printSuffixes(root_);
}

Any solution?

Edit:

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]);
}
srt1104
  • 959
  • 7
  • 11

1 Answers1

1

Default arguments have quite a few restrictions.

Instead, consider using nullptr as the default:

void SuffixTree::printSuffixes(Node* current = nullptr)
{
    if (current == nullptr)
        current = root_;
    // ...
}
Acorn
  • 24,970
  • 5
  • 40
  • 69
  • Why add run time costs? – NathanOliver Oct 25 '19 at 19:32
  • The `printSuffixes(Node*)` function is a recursive function which already has a case to handle when `current ` is `nullptr`. – srt1104 Oct 25 '19 at 19:34
  • @NathanOliver This answer is only providing an easy alternative for cases when a default argument cannot be used due to language restrictions. Whether *that* is the best solution or not, it depends (and sometimes it cannot even be used). In any case, if inlined, there is no runtime cost. – Acorn Oct 25 '19 at 19:34
  • @lucieon Then you should state that in the question (and perhaps give an example of the callers and the bodies of the member functions). It sounds like you are not designing the interface properly. – Acorn Oct 25 '19 at 19:38
  • @Acorn It uses some more variables which are not related to the problem hence I decided not to include it. I can if you want but can you explain how's my interface incorrect? Edit: I added the 2nd overloaded method. – srt1104 Oct 25 '19 at 19:44