错误 从'int*'到'int'的转换无效 -允许

Error invalid conversion from 'int*' to 'int' -fpermissive

本文关键字:int 无效 转换 允许 错误      更新时间:2023-10-16

下面是代码,但我不知道如何解购。有人可以帮助我吗?

在此处输入图像描述

#include <iostream>
using namespace std;


class CSample
{

    int *x;
    int N;

public:

    //dafualt constructor
    CSample(): x(NULL)
    {}          
    void AllocateX(int N)
    {
        this->N = N;
        x = new int[this->N]; 
    }
    int GetX()
    {
        return x;
    }
    ~CSample()
    {
        delete []x;
    }
};
int main()
{
    CSample ob1; //Default constructor is called.
    ob1.AllocateX(10);
    //problem with this line
    CSample ob2 = ob1; //default copy constructor called.
    CSample ob3; //Default constructor called.
    //problem with this line
    ob3 = ob1; //default overloaded = operator function called.
}

此方法具有错误的签名

int GetX()
{
    return x;
}

它应该是

int* GetX()
{
    return x;
}

就您的作业而言,您需要一个复制分配运算符来说出看起来像ob3 = ob1

CSample& operator=(CSample& other)
{
    N = other.N;
    x = new int[N];
    std::copy(other.x, other.x + other.N, x);
    return *this;
}