gpt4 book ai didi

c++ - 为什么我的复制构造函数会出现 "undefined reference to"错误?

转载 作者:行者123 更新时间:2023-11-28 01:42:07 25 4
gpt4 key购买 nike

我以为我已经为我的 initializer-list constructor 正确设置了所有内容,但显然,它还有一些问题。它应该是我的 Matrix 类重载构造函数,它根据这种类型的输入创建一个Matrix:Matrix d = { {1,3}, {5,9}; 我知道我的一个定义是不正确的,但我无法辨认是哪一个。

Matrix::Matrix(const i_list & list){
uint rows = list.size();
uint cols = list.begin()->size();
int i = 0;
mat = new double*[rows];
for(uint m = 0; m < rows; m++){
mat[m] = new double[rows];
}
for(uint n = 0; n < rows; k++){
for(uint w = 0; w < cols; w++){
mat[n][w] = *(list.begin()[n].begin[w] + i);
i++;
}
}

最佳答案

mat[n][w] = *(list.begin()[n].begin[w] + i);基本上是废话。您可以循环输入,这意味着您不需要单独的循环。

Matrix::Matrix(const i_list & list){
mat = new double*[list.size()];
for(auto r = list.begin(); r != list.end(); ++r){
auto row = mat[r - list.begin()] = new double[r->size()];
for(auto c = r->begin(); c != r->end(); ++c){
row[c - r->begin()] = *c;
}
}
}

但是你应该做的是改变mat来自**doublestd::vector<std::vector<double>> ,此时它变成:

Matrix::Matrix(const i_list & list) : mat(list) {}

关于c++ - 为什么我的复制构造函数会出现 "undefined reference to"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46733644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com