Fan
Fan

Reputation: 217

Accessing static member function from friend function

I am using a linked list to implement a set class. In order to hide my struct Node from the users, I put the struct Node declaration into private. Furthermore, I overloaded the operator +, which denotes union operation between two sets. Because I want to implement it by recursion, I overloaded the operator + in the private field called unionMerge. However, it produced an error "error: ‘unionMerge’ was not declared in this scope", but I did put the unionMerge declaration above the operator+ in the Set.cpp file. Could someone help me out?

Set.h is an interface file. Set.cpp is the implementation for the interface.

Here is the interface file, Set.h:

#ifndef SET_H
#define SET_H
#include <iostream>
using namespace std;
class Set{
    public:
    Set();
    friend const Set operator+(const Set& a, const Set& b);
    private:
    struct Node{ //private Node
        int value;
        Node* next;
    };

    Set(Node *p); //private constructor
    Node* list;

    //overload the public operator+ above
    static Node* unionMerge(const Node* p, const Node *q);
};
#endif

Here is the implementation file, Set.cpp:

#include <iostream>
#include "Set.h"

Set::Set():list(nullptr){
}

Set::Set(Node* p){
    list = p;
}

Set::Node* Set::unionMerge(const Node *p, const Node *q){
    // to debug, I return nullptr 
    return nullptr;
}

const Set operator+(const Set& a, const Set& b){

//error: ‘unionMerge’ was not declared in this scope

     return Set(unionMerge(a.list,b.list));

    Set::Node *p = unionMerge(a.list,b.list);  
    Set s;
    s.list = p;
    return s;
}

Upvotes: 0

Views: 117

Answers (1)

Anton Savin
Anton Savin

Reputation: 41301

operator+() is not a member of Set (friends are not members), so if you want to access a static member of Set you have to qualify it:

 return Set(Set::unionMerge(a.list,b.list));

Upvotes: 3

Related Questions