加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C函数隐藏类函数?

发布时间:2020-12-16 03:07:28 所属栏目:百科 来源:网络整理
导读:最小例子 class A{ friend void swap(A first,A second) {} void swap(A other) {} void call_swap(A other) { swap(*this,other); }};int main() { return 0; } g 4.7说: friend.cpp: In member function ‘void A::call_swap(A)’:friend.cpp:7:20: error
最小例子
class A
{
    friend void swap(A& first,A& second) {}
    void swap(A& other) {}
    void call_swap(A& other)
    {
        swap(*this,other);
    }
};

int main() { return 0; }

g 4.7说:

friend.cpp: In member function ‘void A::call_swap(A&)’:
friend.cpp:7:20: error: no matching function for call to ‘A::swap(A&,A&)’
friend.cpp:7:20: note: candidate is:
friend.cpp:4:7: note: void A::swap(A&)
friend.cpp:4:7: note:   candidate expects 1 argument,2 provided

流血线4:

// void swap(A& other) {}

…它工作正常.为什么,如何解决这个问题,如果我想保留我的互换功能的两个变体?

解决方法

我相信这是因为编译器正在尝试在类中查找该函数.这应该是一个简单的更改,使其工作(它在Visual Studio 2012中工作):
class A; // this and the next line are not needed in VS2012,but
void swap(A& first,A& second); // will make the code compile in g++ and clang++

class A
{
    friend void swap(A& first,A& second) {}
    void swap(A& other) {}
    void call_swap(A& other)
    {
        ::swap(*this,other); // note the scope operator
    }
};

int main() { return 0; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读