Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++数组到数组赋值_C++_Arrays - Fatal编程技术网

C++数组到数组赋值

C++数组到数组赋值,c++,arrays,C++,Arrays,我编写了以下employee类: 可以帮助我解决这个问题吗?< /P> < P>不能只使用C++中的=运算符复制整个数组。你有两个选择 过载=操作员 或 使用类似这样的for循环将一个数组的每个元素复制到另一个数组 forint i=0;i> p>而不是C数组,使用C++ STD::数组,像这样: class employee { //... std::array<int, 12> salaries; //... }; employee(int id2, s

我编写了以下employee类:


可以帮助我解决这个问题吗?< /P> < P>不能只使用C++中的=运算符复制整个数组。你有两个选择

过载=操作员 或 使用类似这样的for循环将一个数组的每个元素复制到另一个数组


forint i=0;i> p>而不是C数组,使用C++ STD::数组,像这样:

class employee {
    //...
    std::array<int, 12> salaries;
    //...
};
employee(int id2, string name2, std::array<int, 12> const & array)
{
    //...
}
当然,你也必须包括在内。然后像这样声明构造函数:

class employee {
    //...
    std::array<int, 12> salaries;
    //...
};
employee(int id2, string name2, std::array<int, 12> const & array)
{
    //...
}

或者删除常量&如果您不确定它们是什么或不需要它们。

您不能通过赋值复制数组。您需要单独复制每个元素。使用std::copy

或者使用Borgeader建议的std::vector或std::array,Borgeader通过赋值进行复制。

使用vector类

但要解决你的问题:

整数工资[12]应为整数*工资 employeeint id2,字符串名称2,int数组[12]应该是employeeint id2,字符串名称2,int*数组

但是,您可能会遇到引用已分配内存和segfault之外的内容的问题。
使用向量

使用std::array或std::vector,它们有运算符=重载,这意味着您不必自己编写赋值代码。问题在于构造函数中的参数。工资不能申报为12号。相反,请使用int*工资。但是,是的,你应该使用向量,更好,更好safer@Jarod42谢谢忘记了std::size\t参数。
std::copy(array, array+12, salaries);