Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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++_C++_Visual Studio_C++11_C++14_C++17 - Fatal编程技术网

程序停止-将数组移动到函数C++

程序停止-将数组移动到函数C++,c++,visual-studio,c++11,c++14,c++17,C++,Visual Studio,C++11,C++14,C++17,我有两门课。 第一类-Midgam-构造函数具有以下行: midgam = new Vector[20]; 第二个类-Vector-我在其中创建了一个名为array的数组 程序运行得很好,只是我有点小问题 在程序结束时,我尝试按字母顺序打印,我使用BubbleSort排序。排序工作正常,但交换函数中的某些内容停止 这就是它的样子: void Midgam::Swap(Vector *xp, Vector *yp) { Vector temp = *xp; cout <&l

我有两门课。 第一类-Midgam-构造函数具有以下行:

midgam = new Vector[20];
第二个类-Vector-我在其中创建了一个名为array的数组

程序运行得很好,只是我有点小问题

在程序结束时,我尝试按字母顺序打印,我使用BubbleSort排序。排序工作正常,但交换函数中的某些内容停止

这就是它的样子:

void Midgam::Swap(Vector *xp, Vector *yp) {
    Vector temp = *xp;
    cout << temp.getName() << endl;
    *xp = *yp;
    *yp = temp;
}

void Midgam::bubbleSort() {
    int i, j;
    for (i = 0; i < iterator - 1; i++) {
        for (j = 0; j < iterator - i - 1; j++) {
            if (midgam[j].getName().compare(midgam[j+1].getName()) > 0) {
                Swap(&midgam[j], &midgam[j+1]);
            }
        }
    }
}
Midgam的完整定义:

向量的完整定义:

有人知道它为什么不起作用吗?谢谢

您的向量缺少正确的副本构造函数

Vector temp = *xp;
//NOT EQUAL TO:
//Vector temp;
//temp=*xp;
上面的语句不会调用operator=const Vector&即使有一个等号。以下行正确且等效:

Vector temp(*xp);
原因是这是一个复制初始化-创建了temp,因此必须调用构造函数-尤其是复制构造函数Vectorconst Vector&。您没有显式声明,因此使用了默认值

然后创建一个浅层副本,temp和*xp共享同一个数组,当它们的析构函数都被调用时,第二个析构函数将尝试删除已经删除的内存-未定义的行为至少在调试模式下触发Visual Studio的调试器

解决方案是进行适当的深度复制-创建一个新数组并复制其内容:

#include <algorithm> #Contains std::copy_n
Vector::Vector(const Vector& other)
{
    name=other.name;
    size=other.size;
    //Creates a new array
    array= new unsigned int[size];
    //Copies the array contents
    std::copy_n(other.array,size,array);
    Boo=other.Bool;
}

这也是为什么不使用原始内存的一个主要例子。我知道您正在实现自定义向量,不想对数组使用std::vector,但至少要使用std::unique\u ptr。如果您刚刚这样做了,您就不必像编译器抱怨的那样首先问这个问题,调试器也不必做编译器的工作。

请提供这些类的完整定义。当指针无效时,VisualStudio通常会停止,并显示一条消息。但由于在您提供的代码片段中没有提到数组,所以我不能再多说了。我猜你的复制构造函数会进行浅层复制,交换中的temp变量会破坏xp的数组。请在@Quimby,试着用你这样的评论来写[mcve]。@Yunnosch谢谢你的建议,我不知道你不需要包含链接,而且懒得搜索它。我还认为,直接说明需要什么可能会有更好的成功机会:@Quimby我已经更新了定义。我发现违反了规则。Vector没有复制构造函数,因此使用隐式定义的构造函数。当从另一个向量构造向量时,两个向量最终都指向同一个数组。然后在析构函数中,每个人都试图破坏它。然后,您的程序通过双重销毁的方式显示未定义的行为。@Oz1988添加了复制构造函数:
Vector temp = *xp;
//NOT EQUAL TO:
//Vector temp;
//temp=*xp;
Vector temp(*xp);
#include <algorithm> #Contains std::copy_n
Vector::Vector(const Vector& other)
{
    name=other.name;
    size=other.size;
    //Creates a new array
    array= new unsigned int[size];
    //Copies the array contents
    std::copy_n(other.array,size,array);
    Boo=other.Bool;
}