Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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++_Arrays_Memory_Dynamic - Fatal编程技术网

C++ 放大动态数组(不允许向量,类赋值)

C++ 放大动态数组(不允许向量,类赋值),c++,arrays,memory,dynamic,C++,Arrays,Memory,Dynamic,这是我要问的第一个问题,如有必要,请仔细检查我: 我正在努力解决一个C++课程在学校的问题。我遇到了一个我真的不能理解的错误。在编程方面,我正在迈出我的第一步 任务说: 两个班 使用继承机制 使用动态内存分配存储学生的数据库 一种不使用高级数据结构而扩展数据库的方法 重载已创建类的对象的流运算符 这是我的代码: #include <conio.h> #include <stdio.h> #include <iostream> #include <stri

这是我要问的第一个问题,如有必要,请仔细检查我:

我正在努力解决一个C++课程在学校的问题。我遇到了一个我真的不能理解的错误。在编程方面,我正在迈出我的第一步

任务说:

两个班

使用继承机制

使用动态内存分配存储学生的数据库

一种不使用高级数据结构而扩展数据库的方法

重载已创建类的对象的流运算符

这是我的代码:

#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

class Person
{
protected:

    char Name[20];
    string Surname;
    int Age;

public:

    virtual void whoAmI()=0;

    friend ostream &operator<< (ostream &out_, Person &s);             // stream overl.
    friend istream &operator>> (istream &in_, Person &s);
    friend void resizeArr(Person* oldList, int oldSize, int newSize);  // array enlarge
};

class Student :public Person
{
public:
    Student(){}
    Student(char name[], string surname, int age )
    {
        strcpy(Name, name);
        Surname = surname;
        Age = age;
    }

    virtual void whoAmI()                   // basically replaced by overloaded ostream
    {
        //cout << "I am a student\nMy name is " << name <<" "<< surname << "; I'm "<< age << " years old.";
        cout << Name << endl;
        cout << Surname << endl;
        cout << Age << endl;
    }
};

istream &operator>> (istream &in_, Person &s)   // through reference: stream object and overloading object
{
    cout << "New student record: "<< endl;
    cout << "Name: " << endl;
    in_ >> s.Name;
    cout << "Surname: " << endl;
    in_ >> s.Surname;
    cout << "Age: " << endl;
    in_ >> s.Age;
    cout << endl;
    return in_;
}

ostream &operator<< (ostream &out_, Person &s)
{
    out_ << "Name:\t\t" << s.Name << endl << "Surname:\t" << s.Surname << endl <<"Age:\t\t" << s.Age << endl;
    return out_;
}

void resizeArr(Student* oldList, int oldSize, int newSize)
{
    Student *newList = new Student[newSize];

    for(int i = 0; i < oldSize; i++)            // COPYING
    {
        newList[i]=oldList[i];
    }

    for(int i = oldSize ; i < newSize ; i++)    // init rest as blank students to avoid errors
    {
        newList[i] = Student( "undef" , "undef", 0);
    }

    delete [] oldList;                          // free memory used for old array
    oldList = newList;                          // reset pointer to new array
}

int main()
{
    int initSize = 2;
    int plusSize = 4;

    Student *list1 = new Student[initSize];

    for (int i=0; i<initSize; i++){                  // initialize each cell as a blank student
        list1[i] = Student(  "undef" , "undef", 0);
    }

    for (int i=0; i<initSize; i++)                   // display initial array
    {
        cout << list1[i] << endl << "------------------------------" << endl;  // for the sake of console output clarity
    }

    resizeArr(list1, initSize, plusSize);           // FUNCTION CALL

    cout << endl << "\tEnlarger database: " << endl << endl;                    // for the sake of console output clarity

    for (int i=0; i<plusSize; i++)               // display enlarged array
    {
        cout << list1[i] << endl << "------------------------------" << endl;  // for the sake of console output clarity
    }

    getch();
    return 0;
}
我以前曾使用整数数组原型化过这样一种机制,它很有效。。。现在我因为一个未知的原因撞车了

请给我指一下正确的方向

编辑:

程序编译并运行时,新数组似乎包含旧数组的前两个元素,当它到达第一个新元素时,程序崩溃,内存单元似乎在拖拽我,并保持着笑脸

复制前两个学生对象,第三个元素会导致错误:


问题在于调整大小功能的定义:

void resizeArr(Student* oldList, int oldSize, int newSize)
除非另有说明,否则传递给函数的所有参数都是按值传递的。即使第一个参数是指针,它也只允许修改它所指向的内存,而不允许修改指针本身

您需要将第一个参数的声明更改为Student**(方法中的代码已更改)或将其更改为Student*&。
我怀疑您很幸运,它适用于integer。

您正在将指向学生列表的指针传递给resizeArr例程,即void resizeArrStudent*oldList、int oldSize、int newSize,但不是指向指针的指针。 因此,将新的/不同的内存块分配给resizeArr中的指针将使resizeArr中的变量指向新地址,但传递给resizeArr(即list1)的指针不会更改

我建议将逻辑更改为Student*resizeArrStudent*oldList、int oldSize、int newSize,并将其命名为list1=resizeArrlist1、initSize、plusSize


这类似于void*realloc void*ptr,size\u t size;的签名

崩溃发生在哪里?错误消息是什么?解决此类问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包括一个重现您的问题的示例,以及您在调试器中所做的观察。不允许向量,类分配-为了克服这些IMO。愚蠢的限制,请创建您自己的简单向量类,而不是具有调整大小功能的专业学生或个人类。你会学到很多东西,比如正确的内存管理,另外你还可以在以后的作业中使用这门课。现在,你的程序没有为学生数组释放内存,所以技术上存在内存泄漏。太好了!我现在看到了错误,原来的指针本身没有被修改。