私は非常に単純なコードをC++で書いて、ベクトルの簡単な操作を行っています。これは、ファイルvector.hの内容です:C++のデストラクタで不明瞭なものがたくさんあります
#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED
class Vector {
int *coordinates;
int *size;
public:
Vector(int vector_size);
Vector(int*,int);
~Vector();
void print(void);
Vector operator +(Vector);
};
#endif
、これは実装です(ファイル:vector.cpp):私のmain.cppにから
#include "vector.h"
#include <iostream>
using namespace std;
Vector::Vector(int vector_size) {
coordinates = new int[vector_size];
size = new int;
*size = vector_size;
}
Vector::Vector(int* vector_coordinates, int vector_size){
coordinates = vector_coordinates;
size = new int;
*size = vector_size;
}
void Vector::print(void){
cout << "[";
for (unsigned short int index =0; index<*size; index++){
cout << coordinates[index];
if (index < *size-1){cout << ", ";};
}
cout << "]\n";
}
Vector Vector::operator+ (Vector other) {
Vector temp(*(other.size));
if ((*temp.size)!=(*(this->size))){
throw 100;
}
int* temp_c = new int[*(other.size)];
int* other_c = other.coordinates;
for (unsigned short int index =0; index<*size; index++){
temp_c[index] = coordinates[index] + other_c[index];
}
temp.coordinates = temp_c;
return (temp);
}
Vector::~Vector(){
delete[] coordinates;
delete size;
}
、私は次の操作を行います
#include <iostream>
using namespace std;
#include "vector/vector.h"
const int size = 3;
int main() {
int *xxx = new int[size];
xxx[0]=4; xxx[1]=5; xxx[2]=-6;
Vector v(xxx,size);// v = [4, 5, -6]
Vector w(size);// w is a vector of size 3
w = v+v; // w should be w=[8,10,-12]
w.print();
return 0;
}
結果がある:
[148836464、5、-6、17、148836384、0、0、17、0、0、0、17、3、0、0、17、0 、0,0,17,1488セグメンテーションフォールト
デストラクタから2行を削除した場合:
delete[] coordinates;
delete size;
すべてが期待どおりに動作し、プログラムの出力:
[8, 10, -12]
私はどんな説明をいただければ幸いです...
アップデート1:私は目に私のオペレータ+方法を変更e。次が、問題が解決されなかった。
Vector Vector::operator+(Vector other) {
int size_of_other = *(other.size);
int size_of_me = *(this->size);
if (size_of_other != size_of_me) {
throw 100;
}
int* temp_c = new int[size_of_me];
int* other_c = other.coordinates;
for (unsigned short int index = 0; index < size_of_me; index++) {
temp_c[index] = coordinates[index] + other_c[index];
}
Vector temp(temp_c,size_of_me);
return (temp);
}
アップデート2:
Vector Vector::operator+(Vector other);
私は望ましい結果を得ないだろう:私は演算子を使用していることに気づきました。それは仕事をした修正された:
const Vector& Vector::operator+(const Vector& other) {
Vector temp(other.size);
for (unsigned short int index = 0; index < size; index++) {
cout << "("<< index <<") "<<coordinates[index] << "+"
<<other.coordinates[index] << ", "<< endl;
temp.coordinates[index] = coordinates[index] + other.coordinates[index];
}
return (temp);
}
アップデート3:更新#2の後、私は地元の「TEMP」を返し、コンパイラから警告を得ていました。私は(私は一時のコピーを返す)完全にすべての問題を解決し、以下に私のコードを変更し、正常に動作します:
int* temp_c = new int;
...
temp_c[index] =
あなたは、次のとおりです。
const Vector Vector::operator+(const Vector& other) const{
Vector temp(other.size);
for (unsigned short int index = 0; index < size; index++) {
temp.coordinates[index] = coordinates[index] + other.coordinates[index];
}
return *(new Vector(temp));
}
'[]座標を削除し、私はそれを修正し、' –
@JamesMcLaughlinを。しかし、再び...同じ!私は私の質問でそれを更新します。 –
メンバー 'size'がポインタになる理由はありません。ポインタはあなたの仕事をより困難にします。 – aschepler