在c ++中可能实现的转换 [英] possible implementations of casting in c++

查看:97
本文介绍了在c ++中可能实现的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标头中有这段程式码片段

  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;

}

我需要转换这部分代码: / p>

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



A * pa1 = new A(a2) / p>

  assert(i == * pa1); 

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

解决方案

您的错误与投放或用户定义的转化无关。



您不能对不是成员函数的函数使用const限定符,因此:

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

应该是这样:

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


I have this snippet of the code in my 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;
};

implementation of the operator==

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

}

and I need casting for this part of my code:

i - is some int, which my function receives

A *pa1 = new A(a2);

assert(i == *pa1);

I receive an error non-member function, How can I fix it? thanks in advance

解决方案

Your error is nothing to do with casting or user-defined conversions.

You can't have a const qualification on a function that isn't a member function so this:

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

should be this:

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

这篇关于在c ++中可能实现的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆