C++ 而循环正在更改每个对象,而不是遍历

C++ 而循环正在更改每个对象,而不是遍历,c++,vector,matrix-multiplication,C++,Vector,Matrix Multiplication,这就是实现: result[j]->coordinate[i] = (*tmp[i] * *v.columns[j]) for(自动i=0;idimension.rows;i++){ for(auto j=0;jdimension.rows;k++){ 结果[j]->坐标[i]+=this->columns[i]->坐标[k]*v.columns[k]->坐标[j]; printf(“j=%d,%s和%s\n”,j,结果[j]->printV(),结果[0]->printV()); }

这就是实现:

 result[j]->coordinate[i] = (*tmp[i] * *v.columns[j])
for(自动i=0;idimension.rows;i++){
for(auto j=0;jdimension.rows;k++){
结果[j]->坐标[i]+=this->columns[i]->坐标[k]*v.columns[k]->坐标[j];
printf(“j=%d,%s和%s\n”,j,结果[j]->printV(),结果[0]->printV());
}
}
}   
当j=1时,结果[0]也会改变,我不知道为什么。这是输出

for(auto i=0; i < this->dimension.rows;i++){
            for(auto j=0; j<v.dimension.columns;j++){
                for(auto k=0; k< this->dimension.rows;k++){
                    result[j]->coordinate[i] += this->columns[i]->coordinate[k] * v.columns[k]->coordinate[j];
                    printf("j = %d, %s and %s\n",j,result[j]->printV(), result[0]->printV());
                }
            }
        }   
j=0,且
j=0,且
j=1,且
j=1,且
j=2,且
j=2,且
j=0,且
j=0,且
j=1,且
j=1,且
j=2,且
j=2,且

矩阵乘法的定义不同。它需要3个嵌套for循环,而不仅仅是两个:

对于每对
(i,j)
,需要迭代
k
的所有值,并执行以下伪代码:

j = 0, <2.000000,0.000000> and <2.000000,0.000000>
j = 0, <2.000000,0.000000> and <2.000000,0.000000>
j = 1, <2.000000,0.000000> and <2.000000,0.000000>
j = 1, <1.000000,0.000000> and <1.000000,0.000000>
j = 2, <1.000000,0.000000> and <1.000000,0.000000>
j = 2, <1.000000,0.000000> and <1.000000,0.000000>
j = 0, <1.000000,-1.000000> and <1.000000,-1.000000>
j = 0, <1.000000,-1.000000> and <1.000000,-1.000000>
j = 1, <1.000000,-1.000000> and <1.000000,-1.000000>
j = 1, <1.000000,1.000000> and <1.000000,1.000000>
j = 2, <1.000000,1.000000> and <1.000000,1.000000>
j = 2, <1.000000,1.000000> and <1.000000,1.000000> 

printV函数在哪里???
strncat(s
导致UB,
strncat
需要以字符串开头
snprintf
被调用时长度参数大于缓冲区大小(尽管在这种特殊情况下不太可能溢出)
strncat
3参数在每次调用中都是不正确的;所有这些参数都可能是
strcat
(尽管
snprintf
会更高效、更整洁)
操作符*
(以及其他操作符)应该按值返回;您现在这样做会导致内存泄漏。根本不要使用
new
。我已经编辑了原始帖子来实现您的伪代码。它仍然不起作用。我犯了一个错误。现在编辑了我的帖子。它仍然会给出相同的问题,即向量结果的第一个坐标[0]正在变化。我想你把结果(I,j)和结果(j,I)混淆了。当j=1和k=1时似乎会发生这种情况。结果(I,j)是指第I行和第j列?
for(auto i=0; i < this->dimension.rows;i++){
            for(auto j=0; j<v.dimension.columns;j++){
                for(auto k=0; k< this->dimension.rows;k++){
                    result[j]->coordinate[i] += this->columns[i]->coordinate[k] * v.columns[k]->coordinate[j];
                    printf("j = %d, %s and %s\n",j,result[j]->printV(), result[0]->printV());
                }
            }
        }   
j = 0, <2.000000,0.000000> and <2.000000,0.000000>
j = 0, <2.000000,0.000000> and <2.000000,0.000000>
j = 1, <2.000000,0.000000> and <2.000000,0.000000>
j = 1, <1.000000,0.000000> and <1.000000,0.000000>
j = 2, <1.000000,0.000000> and <1.000000,0.000000>
j = 2, <1.000000,0.000000> and <1.000000,0.000000>
j = 0, <1.000000,-1.000000> and <1.000000,-1.000000>
j = 0, <1.000000,-1.000000> and <1.000000,-1.000000>
j = 1, <1.000000,-1.000000> and <1.000000,-1.000000>
j = 1, <1.000000,1.000000> and <1.000000,1.000000>
j = 2, <1.000000,1.000000> and <1.000000,1.000000>
j = 2, <1.000000,1.000000> and <1.000000,1.000000> 
result = zeros(a.rows,b.cols)
for i = 0:a.rows-1
   for j = 0:b.cols-1
      for k = 0:a.cols-1
         result(i,j) += a(i,k) * b(k,j)