Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 动态内存问题(_CrtIsValidHeapPointer)_C++_Visual Studio_Segmentation Fault_Dynamic Memory Allocation - Fatal编程技术网

C++ 动态内存问题(_CrtIsValidHeapPointer)

C++ 动态内存问题(_CrtIsValidHeapPointer),c++,visual-studio,segmentation-fault,dynamic-memory-allocation,C++,Visual Studio,Segmentation Fault,Dynamic Memory Allocation,我不确定为什么我的程序是seg。每次我使用Visual Studio 2015编译器运行它时都会出错,但使用GNU编译器编译时效果很好。有人能提供这个问题的见解吗 这通常包含在名为EVector.h的文件中,但我在下面添加了它,以备需要 #include<iostream> class EVector { public: EVector(); ~EVector(); //Operator Overloading friend std::istrea

我不确定为什么我的程序是seg。每次我使用Visual Studio 2015编译器运行它时都会出错,但使用GNU编译器编译时效果很好。有人能提供这个问题的见解吗

这通常包含在名为EVector.h的文件中,但我在下面添加了它,以备需要

#include<iostream>

class EVector
{
public:
    EVector();
    ~EVector();

    //Operator Overloading
    friend std::istream& operator>>(std::istream&, EVector&); //Input Stream
    friend std::ostream& operator<<(std::ostream&, EVector&); //Output Stream
    EVector operator=(EVector&);

private:
    double* Tuples;
    int dimension;
};
EVector.cpp的其余部分

istream& operator>>(istream& instream, EVector& vector) {
    cout << "Enter the dimension of the Euclidian Vector: ";
    instream >> vector.dimension;

    vector.Tuples = new double[vector.dimension];
    cout << "Enter the Tuple's Values (The program will take values until all dimensions are full)" << endl;

    //Take in Tuples values
    for (int x = 0; x < vector.dimension; x++) {
        cout << "Enter a Value (" << (vector.dimension - x) << " value(s) left): ";
        instream >> vector.Tuples[x];
    }

    return instream;
}
istream&operator>>(istream&instream,EVector&vector){
cout>vector.dimension;
vector.Tuples=新的双精度[vector.dimension];

cout可能是双重删除。您的深度副本将复制您的EVector元素。 但它们包含一个指针。 如果对这两个函数都调用析构函数,它将出现故障。
复制时使Toubles指针无效,或者执行真正的deepcopy并调用额外的新[]用于在复制的对象中分配Tubles指针。

没有雪茄。感谢您的快速响应。还有其他可能的想法吗?哦,不,我太蠢了。将指针设置为NULL没有帮助,因为有一个元素的副本,它仍然包含一个集合指针。在析构函数中打印指针地址,您将看到什么我的意思是。或者在深度副本中使源的Tubles指针无效。
EVector::~EVector() {
    if (Tuples != NULL) {
        delete[] Tuples;
    }
}
istream& operator>>(istream& instream, EVector& vector) {
    cout << "Enter the dimension of the Euclidian Vector: ";
    instream >> vector.dimension;

    vector.Tuples = new double[vector.dimension];
    cout << "Enter the Tuple's Values (The program will take values until all dimensions are full)" << endl;

    //Take in Tuples values
    for (int x = 0; x < vector.dimension; x++) {
        cout << "Enter a Value (" << (vector.dimension - x) << " value(s) left): ";
        instream >> vector.Tuples[x];
    }

    return instream;
}
ostream& operator<<(ostream& outstream, EVector& vector) {
    outstream << "Dimension: " << vector.dimension << endl;
    outstream << "Tuple's Values: ( ";
    for (int x = 0; x < vector.dimension; x++) {
        outstream << vector.Tuples[x] << " ";
    }
    outstream << ")";

    return outstream;
}
EVector EVector::operator=(EVector& vector) {
    if (this == &vector) {
        return *this;
    }

    if (Tuples != NULL) {
        delete[] Tuples;
    }

    dimension = vector.dimension;
    Tuples = new double[dimension];

    for (int i = 0; i < dimension; i++) {
        Tuples[i] = vector.Tuples[i];
    }

    return *this;
}