C++ c+中的矩阵类+;?
我一直在尝试编写一个矩阵类,我正在使用一个main方法来测试它。它不起作用了。。。一点也不 我不明白为什么大小(AllRowValue的大小除以double的大小)为零 我一直在写一些调试打印,但没有帮助。。。我真的,真的很新,用C++,所以任何和所有的帮助/建议将被赞赏。C++ c+中的矩阵类+;?,c++,matrix,C++,Matrix,我一直在尝试编写一个矩阵类,我正在使用一个main方法来测试它。它不起作用了。。。一点也不 我不明白为什么大小(AllRowValue的大小除以double的大小)为零 我一直在写一些调试打印,但没有帮助。。。我真的,真的很新,用C++,所以任何和所有的帮助/建议将被赞赏。 1 #include "matrix.h" 2 #include <iostream> 3 #include <sstream> 4 5 Matrix::Matrix(){ 6 };
1 #include "matrix.h"
2 #include <iostream>
3 #include <sstream>
4
5 Matrix::Matrix(){
6 };
7
8 Matrix::Matrix(int rows, int columns, double allRowValues [] ){
9 this->rows = rows;
10 this->columns = columns;
11 this->matrixValues = new double[rows*columns];
13 std::cout <<"ALL ROW VALUES" <<std::endl;
14 std::cout<<"*****" <<std::endl;
15 std::cout << sizeof (allRowValues) << std::endl;
16 std::cout<<"*****" <<std::endl;
17 std::cout << sizeof(allRowValues[0]) << std::endl;
18 std::cout<<"*****" <<std::endl;
19 int size = sizeof(allRowValues)/sizeof(double);
20 int numberOfValues = rows * columns;
21 int currentIndex = 0;
22 for (int i = 0; i < numberOfValues; i++){
23 std::cout<< "MATRIX CONSTRUCTOR\n";
24 std::cout<<allRowValues <<std::endl;
25 std::cout<<"-----"<<std::endl;
26 std::cout<<index << std::endl;
27 std::cout<<"-----"<<std::endl;
28 std::cout<<size << std::endl;
29 std::cout<<"-----"<<std::endl;
30 if ((allRowValues) && (currentIndex < size)){
31 std::cout << "ARV " <<std::endl;
32 std::cout << allRowValues[currentIndex] << std::endl;
33 this->matrixValues[i]= allRowValues[currentIndex];
34 std::cout << "MAT " << allRowValues[currentIndex++] << std::endl;
35 }else{
36 std::cout << "Else\n";
37 }
38 }
39 int index=0;
40 for (int j = 0; j < rows; j++){
41 for (int i = 0; i < columns; i++){
42 std::cout << this->matrixValues[index++];
43 }
44 std::cout<<std::endl;
45 }
46 };
47
48 Matrix::Matrix(double* rowValues){
49 int sizeRows = sizeof(rowValues)/sizeof(double);
50 //TODO: throw error for all rows must be the same length
51 this->rows = sizeRows;
52 int sizeColumns = sizeof(rowValues[0])/sizeof(double);
53 this->columns = sizeColumns;
54 this->matrixValues = rowValues;
55 };
56
57 double Matrix::width(){
58 std::cout << "Width\n";
59 return this->columns;
60 };
61
62 double Matrix::height(){
63 std::cout << "Height\n";
64 return this->rows;
65 };
66
67 std::string Matrix::toString(){
68 int numberOfValues = 0;
69 std::cout<<matrixValues[numberOfValues];
70 std::string build_output;
71 std::cout<<matrixValues;
72 for (int i = 0; i < rows; i++){
73 build_output = "[";
74 std::cout << "\n";
75 for (int j = 0; j < columns; j++){
76 std::cout << "VALUE: " <<matrixValues[numberOfValues];
77 build_output = matrixValues[numberOfValues];
78 numberOfValues++;
79 }
80 build_output = " ]";
81 }
82 return build_output;
83 }
84
85 int main (){
86 double values[6] = {1, 2, 3, 4, 5, 6};
87 std::cout <<"Values: \n";
88 Matrix a = Matrix(2, 3, values);
89 std::cout << a.width() << std::endl;
90 std::cout << a.height() << std::endl;
91 std::cout << a.toString();
92 return 1;
93 }
1#包括“matrix.h”
2#包括
3#包括
4.
5矩阵::矩阵(){
6 };
7.
8矩阵::矩阵(int行、int列、双allRowValues[])){
9本->行=行;
10此->列=列;
11此->矩阵值=新的双精度[行*列];
13 std::coutdouble allRowValues[]
声明一个指针,而不是数组。表达式sizeof(allRowValues)/sizeof(double)
然后计算指针大小与double
大小之间的比率。如果double更大,结果显然为零
出于某种原因,在另一个构造函数中也会发生相同的错误(sizeof(rowValues)/sizeof(double)
),但这次参数显然是一个指针
这是double数组(即double)中大小为1的元素与double的大小(显然是1)之间的比率
似乎有一种观点认为,sizeof
操作符可以神奇地知道一个数组的大小,只要给它的第一个元素一个指针。它不能。或者数组和指针之间可能有混淆。它们是
大多数情况下,数组(即T[N]
类型的对象,如double[100]
)只是衰减为指向其第一个元素的指针(即T*
,如double*
),在过程中丢失了大小信息。这就是为什么如果要“传递数组”,就永远不能传递指针的原因.您需要以某种方式传递大小信息
您可以显式地将大小作为额外参数传递,或者传递另一个标记缓冲区结束的指针(迭代器样式)。您还可以传递对数组的引用(这可以防止衰减为指针,从而保留大小信息),并使用模板获取大小信息
template <std::size_t N, std::size_t M>
void pass_a_2d_array_by_reference(double(&the_array)[N][M]) { // N and M are the sizes
// do stuff
}
模板
void pass_a_2d_array_by_reference(double(&the_array)[N][M]){//N和M是大小
//做事
}
现在您已经了解了这些问题,如果使用现成的解决方案,您根本无法解决这些问题:
template<int size>
Matrix::Matrix(int rows, int columns, double (&allRowValues) [size] ){
...
}
:如果您不需要连续存储,这是一个非常好的解决方案。如果您想要锯齿状阵列,这也是最好的解决方案std::vector
:另一个非常好的解决方案,适用于更高维度的阵列boost::multiarray
- 还有许多其他现有的解决方案,可以满足各种需求。请随便看看
double allRowValues[]
声明一个指针,而不是数组。表达式sizeof(allRowValues)/sizeof(double)
然后计算指针大小与double
大小之间的比率。如果double更大,结果显然为零
出于某种原因,在另一个构造函数中也会发生相同的错误(sizeof(rowValues)/sizeof(double)
),但这次参数显然是一个指针这是double数组(即double)中大小为1的元素与double的大小(显然是1)之间的比率
似乎有一种观点认为,sizeof
操作符可以神奇地知道一个数组的大小,只要给它的第一个元素一个指针。它不能。或者数组和指针之间可能有混淆。它们是
大多数情况下,数组(即T[N]
类型的对象,如double[100]
)只是衰减为指向其第一个元素的指针(即T*
,如double*
),在过程中丢失了大小信息。这就是为什么如果要“传递数组”,就永远不能传递指针的原因.您需要以某种方式传递大小信息
您可以显式地将大小作为额外参数传递,或者传递另一个标记缓冲区结束的指针(迭代器样式)。您还可以传递对数组的引用(这可以防止衰减为指针,从而保留大小信息),并使用模板获取大小信息
template <std::size_t N, std::size_t M>
void pass_a_2d_array_by_reference(double(&the_array)[N][M]) { // N and M are the sizes
// do stuff
}
模板
void pass_a_2d_array_by_reference(double(&the_array)[N][M]){//N和M是大小
//做事
}
现在您已经了解了这些问题,如果使用现成的解决方案,您根本无法解决这些问题:
template<int size>
Matrix::Matrix(int rows, int columns, double (&allRowValues) [size] ){
...
}
:如果您不需要连续存储,这是一个非常好的解决方案。如果您想要锯齿状阵列,这也是最好的解决方案std::vector
:另一个非常好的解决方案,适用于更高维度的阵列boost::multiarray
- 还有许多其他现有的解决方案,可以满足各种需求。请随便看看
std::vector&
(或std::array)而不是c样式的数组double allRowValues[]
。因此,您可以使用allRowValues.size()轻松获得其大小
c++常见问题解答精简版:
参考资料:
您应该使用std::vector&
(或std::array)而不是c样式的数组double allRowValues[]
。因此,您可以使用allRowValues.size()轻松获得其大小
c++常见问题解答精简版:
参考资料:
如果您坚持使用c型阵列(我不推荐使用),您也可以使用模板化解决方案:
template<int size>
Matrix::Matrix(int rows, int columns, double (&allRowValues) [size] ){
...
}
模板
矩阵::矩阵(int行、int列、double(&allRowValues)[size]){
...
}
总的来说,我推荐一个现成的、允许使用的开源矩阵库,如。如果您坚持使用c风格的阵列(我不推荐),您还可以使用模板化解决方案:
template<int size>
Matrix::Matrix(int rows, int columns, double (&allRowValues) [size] ){
...
}