홈>
저는 Java 개발자이지만 이제는 C ++ 라이브러리가 필요하며이 언어에 익숙하지 않습니다. 특히 포인터, 참조 및 메모리 할당에 대해 항상 혼란스러워합니다. 이것이 내가 개발중인 매트릭스 클래스에서 오류가 발생하는 이유라고 생각합니다.
기본 코드 :
#include "stdafx.h"
#include "matrix.cpp"
void matrixtest();
int main()
{
matrixtest();
system("pause");
return 0;
}
void matrixtest()
{
// let's try 3x3 matrices
static const int arr1[] = {1, 2, 1, -1, 1, 2, 2, 3, -4};
static const int arr2[] = {0, 2, 2, 1, -1, 0, 3, 2, -2};
vector<int> values1(arr1, arr1 + sizeof(arr1) / sizeof(arr1[0]));
vector<int> values2(arr2, arr2 + sizeof(arr2) / sizeof(arr2[0]));
matrix A(values1, 3);
matrix B(values2, 3);
matrix sum = A + B;
sum.show();
matrix diff = A - B;
diff.show();
matrix prod = A * B;
prod.show();
}
matrix.cpp
흥미로운 코드 :
matrix::matrix(vector<int> v, int r) : values(v), rows(r) {
values = v;
rows = r;
}
// [...]
matrix& matrix::operator += (const matrix& rhs) {
matrix result = (*this) + rhs;
(*this) = result;
return *this;
}
matrix matrix::operator + (const matrix& rhs) {
if (rows != rhs.rows || values.size() != rhs.values.size()) {
throw std::length_error("Matrices shapes mismatch");
}
matrix result(values, rows);
for (auto& i : values) {
result.values[i] = this->values[i] + rhs.values[i];
}
return result;
}
// [...]
void matrix::show() {
string delimiter = "";
for (auto& i : values) {
delimiter = "";
for (auto j = 0; j < values.size()/rows; j++) {
cout << delimiter << values[i * values.size()/rows + j]; // this is the line giving the error
delimiter = ",";
}
std::cout << std::endl;
}
}
전체
matrix.hpp
파일 :
#ifndef matrix_hpp
#define matrix_hpp
class matrix {
private:
std::vector<int> values; // size is found by values.size()
int rows; // columns is values.size()/rows
public:
matrix(vector<int>, int); // base ctor.
matrix(const matrix& rhs); // copy ctor.
matrix& operator=(const matrix& rhs); // assign. ctor.
~matrix(); // dtor.
int& operator () (int row, int column);
const int& operator () (int row, int column) const;
matrix operator + (int scalar) const;
matrix operator - (int scalar) const;
matrix operator * (int scalar) const;
matrix& operator += (int scalar);
matrix& operator -= (int scalar);
matrix& operator *= (int scalar);
matrix operator + (const matrix&);
matrix operator - (const matrix&);
matrix operator * (const matrix&);
matrix& operator += (const matrix&);
matrix& operator *= (const matrix&);
// should be private ??
void reshape(int newRows, int newColumns);
void show(); //used for dev. only
void range(int start, int defaultStep = 1);
void fill(int value);
void randint(int lowerBound, int upperBound);
};
#endif /* CMatrix_hpp */
이 클래스는 행렬 예제 의 예제를 기반으로합니다.
오류에 '0xC0000005 : 액세스 위반 읽기 위치 0x5820A694'가 표시됩니다. 따라서 메모리 할당이 잘못되었거나 범위를 벗어난 배열이 있거나 '&'연산자를 엉망으로 생각합니다.
편집 : 나는 다음과 같은 흔적을 얻는다 :
- 이 0x00dffe24 {values = {size = 9} rows = 3} matrix *
매트릭스가 존재하지만 어떤 이유로 든 오류가 발생합니다.
- 답변 # 1
관련 자료
- c# - Visual Studio의 오류 CS0311 일반 오류
- c++ - Visual Studio Code에서 SFML 코드를 디버깅하는 동안 오류가 발생했습니다
- 명명 규칙을 사용하여 특정 파일을 삭제하기위한 Visual Studio 사전 빌드 이벤트
- python - Visual Studio 2019가 탭을 공백으로 변경하지 못하도록하는 방법은 무엇입니까?
- wpf : Visual Studio Professional 2019에 .NET Framework 5.0을 어떻게 추가 할 수 있습니까?
- php - Android Studio에서 데이터를 데이터베이스로 보내는 오류 1048
- c++ - 비주얼 스튜디오 - / clr 옵션 => 함수로 임베디드 코드를 컴파일하는 c ++ 14는 관리 대상으로 컴파일 할 수 없습니다 c2711, c3820
- npm - Visual Studio Code 확장 생성기가 작동하지 않음
- android studio에서 유효성 검사 오류가 발생했습니다
- Android Studio 411 macOS 전체 화면 오류
- c++ - Visual Studio에서 파일을 열 수 없습니다
- C ++ Microsoft Visual Studio 배열 문제
- Visual Studio 2019 Publish에서 경고를 오류로 처리 함
- c# - Visual Studio가 Microsoft SQL Server Management Studio에서 데이터를로드하지 않음
- .net - Blazor 및 Visual Studio의 InputFile은 어디에 있습니까?
- java - Android Studio, 'cannot resolve method'오류 발생
- 디버깅 할 때 Visual Studio Code g ++가 c++ 17로 컴파일되지 않음
- msbuild - 사용자 지정 빌드 단계 만있는 Visual Studio 프로젝트 (기본 빌드 없음)
- vsix - Visual Studio "Clean Solution"명령 직후에 코드를 실행할 수 있습니까?
- angular - angular9를 istall 수 없습니다… !!! Visual Studio에서 git 로그인을 요청합니까?
관련 질문
- X Visual Studio에 필요한 구성 요소가 없습니다. "C++를 사용한 데스크톱 개발"용 Visual Studio 설치 프로그램을 다시 실행하십시오.
- c++ : USN 저널 쿼리의 전체 경로를 얻는 방법은 무엇입니까?
- c++ : pcl::PointCloud를 Eigen::MatrixBase로 변환
- MFC C++ 6.0 응용 프로그램에서 VS 2005로 마이그레이션
- c++ : Visual Studio로 빌드한 EXE 파일을 다른 컴퓨터에서 실행할 때 발생하는 문제
- c++ : VS Community에서 작동하는 프로젝트가 CLion에서 작동하지 않음
- c++ : CommonAPI CMake 오류
- c++ : Linux 프로젝트 및 Windows 프로젝트 기능을 하나로 통합
- c++ : Visual Studio 디버거 플래그가 없을 때 오류가 발생합니다.
- c++ : 곱셈의 잘못된 결과: 정의되지 않은 동작 또는 컴파일러 버그?
오류가 발생하기 전에 사용중인 for 루프
는 범위 기반 for 루프입니다. 이것으로 vector (
values
)에 존재하는 값을 얻을 수 있습니다 ).그러나 여기에 원하는 것을 작성한 논리에 따라 작업중 인 행을 나타내는 인덱스가 있습니다. 일반적인 for 루프로 가야합니다.