2017-04-12 73 views
-1

下面是代码,但我不知道如何使用它。有人能帮我吗?错误无效从'int *'转换为'int'-fpermissive

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. 
} 
+0

GetX应该是int *类型或返回int类型。您也没有定义复制构造函数。请确保在将来正确设置问题的格式,并提供最小,完整和可验证的示例。 –

+0

“我不知道如何调试” - 请看看[如何调试小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ )由Eric Lippert撰写,它是关于调试技术的*优秀*文章。 – EJoshuaS

回答

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; 
} 
+0

这是真的,但它并没有解释OP的说法是问题出现在作业线上。实际上,这不是她的说法,而是谁写的代码OP不正确地复制的声明,抱歉。其实,你是对的。 – davidbak

+0

将有助于解释错误消息如何准确地说明这一点。 – Caleth

+0

感谢您的帮助〜^^ – sue

相关问题