저는 Matrix 클래스를 작성하려고 노력해 왔습니다. 그리고 그것을 테스트하기 위해 main 메소드를 사용하고 있습니다. 그것은 전혀 작동하지 않습니다 ... 전혀.C++의 Matrix 클래스?
size (allRowValues의 크기를 double 크기로 나눈 값)가 0 인 이유를 알지 못합니다!
저는 디버그 프린트를 작성했지만 도움이되지 않습니다 ... 저는 정말 C++로 새롭고 모든 도움/조언을 부탁드립니다.
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 }
이 숙제입니까? 그렇지 않다면 C++ 용으로 작성된 매우 훌륭한 행렬 라이브러리가 많이 있습니다.이 라이브러리는 직접 처리하는 것보다 훨씬 빠르고 덜 버그가 있습니다. – Shep
직접 해보기로 결정한 경우에도 사용자가 대화식으로 데이터를 입력하도록 강요하지 마십시오. 파일에 데이터를 넣고 파일 이름을 지정하십시오. 이렇게하면 코드가 단순 해지고 사용자에게 오류가 발생하기 쉬워집니다. –
'sizeof'는 나쁜 디자인입니다. 컴파일러가 32 비트에서 8 바이트를, 64 비트에서 16 바이트를 선택하면 어떻게 될까요? 'sizeof'에 의존하지 마십시오. – CppLearner