【发布时间】:2012-04-06 13:37:39
【问题描述】:
我用 C++ 编写了一些非常简单的代码来对向量进行一些简单的操作。这是文件vector.h的内容:
#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):
#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;
}
从我的 main.cpp 中,我执行以下操作:
#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, 148836480, 0, 0 , 17, 0, 10, -12, 135025, 0, 0, 0, 0, 0, 0, , 0, 0,分段错误
如果我从析构函数中删除这两行:
delete[] coordinates;
delete size;
一切正常,程序输出:
[8, 10, -12]
我会很感激任何解释...
更新1:我将operator+方法改成如下,但问题没有解决:
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 之后,我收到了来自编译器的警告,我返回了本地“临时”。我将我的代码更改为以下,它完全解决了所有问题并且工作正常(我返回一个 copy 的 temp):
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));
}
【问题讨论】:
-
delete [] coordinates; -
@JamesMcLaughlin 我更正了。但是,再一次……同样的!我会在我的问题中更新它。
-
成员
size没有理由成为指针。指针会让你的工作更难。 -
使用
std::unique_ptr会突出显示您的几个错误。 -
刚刚发布了一个完整的工作实现,包括我已经验证的复制构造函数和赋值运算符。看看我们一直在建议什么...
标签: c++ pointers destructor