繁体   English   中英

从好友函数访问静态成员函数

[英]Accessing static member function from friend function

我正在使用链表来实现集合类。 为了向用户隐藏我的结构节点,我将结构节点声明设为私有。 此外,我重载了运算符+,表示两个集合之间的并集运算。 因为我想通过递归实现,所以我在称为unionMerge的私有字段中重载了运算符+。 但是,它产生了一个错误“错误:在此范围内未声明'unionMerge'”,但是我确实将unionMerge声明放在Set.cpp文件中的operator +上方。 有人可以帮我吗?

Set.h是一个接口文件。 Set.cpp是该接口的实现。

这是接口文件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

这是实现文件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;
}

operator+()不是Set的成员(朋友不是成员),因此,如果要访问Set的静态成员,则必须对其进行限定:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM