错误 从'int*'到'int'的转换无效 -允许
Error invalid conversion from 'int*' to 'int' -fpermissive
下面是代码,但我不知道如何解购。有人可以帮助我吗?
在此处输入图像描述
#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;
}
相关文章:
- 错误:从"int"到枚举c++的转换无效
- 多维数组 C++ 中数组下标的类型"int[int]"无效
- 从 'int' 到 'int*' CPP 的转换无效
- 无效 f(int) 的模板和 decltype
- 错误:从"int"到"int*"的转换无效[-允许].我在下面提供了我的代码,我
- 错误:数组下标的类型"int[int]"无效
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 从类指针到 int 的转换无效
- C++ 错误:由于 swprintf 而从 'int' 到 'const wchar_t* 的转换无效
- 为什么 std::d eclval<int>() = std::d eclval<int>() 无效?
- 错误:数组下标的类型"const bool[int]"无效
- 2D数组中数组下标的类型"int[int]"无效
- 错误:数组下标的类型'int[int]'无效
- C++中的合并排序:无法在合并函数中传递数组 - 转换为'int*'无效
- 数组下标的类型"int[int]"无效
- 使用矢量的数组下标'int[int]'无效类型
- C++中的二进制补码 - 数组下标的类型"int[int]"无效
- 从“char*”到“char”[-fpermissive]的转换无效,数组下标的类型“char[int]”无效
- 无法解决错误:间接寻址需要指针操作数("int"无效)
- C++将数组相互分配;type int*=type int有效,但int=int*无效