gpt4 book ai didi

c++ - 在 C++ 中转换的可能实现

转载 作者:行者123 更新时间:2023-11-27 23:33:29 25 4
gpt4 key购买 nike

我的 header 中有这段代码:

class A {
private:
int player;
public:
A(int initPlayer = 0);
A(const A&);
A& operator=(const A&);
~A();
void foo() const;
friend int operator==(const A& i, const A& member) const;
};

运算符的实现==

int operator==(const A& i, const A& member) const{
if(i.player == member.player){
return 1;
}
return 0;

}

我需要对这部分代码进行转换:

i - 是一些 int,我的函数接收它

A *pa1 = 新 A(a2);

assert(i == *pa1);

我收到一个错误非成员函数,我该如何解决?提前致谢

最佳答案

您的错误与转换或用户定义的转换无关。

您不能对非成员函数的函数进行 const 限定,因此:

int operator==(const A& i, const A& member) const;

应该是这样的:

int operator==(const A& i, const A& member);

关于c++ - 在 C++ 中转换的可能实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3043219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com