错误转换为' int *'到' int' -fpermissive

时间:2017-04-12 16:37:14

标签: c++

以下是代码,但我不知道如何去除它。有人能帮助我吗?

enter image description here

#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.
}

1 个答案:

答案 0 :(得分:1)

此方法具有错误的签名

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;
}
相关问题