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/12.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++;_C++_Arrays_Dynamic_Copy Constructor - Fatal编程技术网

C++ C++;

C++ C++;,c++,arrays,dynamic,copy-constructor,C++,Arrays,Dynamic,Copy Constructor,我正在尝试在我的List类中创建一个动态数组,该数组将以2的大小开始,当您使用insert方法插入值时,它将检查是否有足够的空间,如果没有,它将以+2的大小调整数组大小。。。问题是它正在崩溃,VS正在抱怨堆的损坏。另外,我认为我的复制构造函数没有被调用,因为cout没有显示: list.h文件: class List { public: // DEFAULT Constructor List(); // Deconstructor to free memory all

我正在尝试在我的List类中创建一个动态数组,该数组将以2的大小开始,当您使用insert方法插入值时,它将检查是否有足够的空间,如果没有,它将以+2的大小调整数组大小。。。问题是它正在崩溃,VS正在抱怨堆的损坏。另外,我认为我的复制构造函数没有被调用,因为cout没有显示:

list.h文件:

class List
{
public:

    //  DEFAULT Constructor
    List();
    // Deconstructor to free memory allocated 
    ~List();// Prevent memory leaks

    // COPY Constructor for pointers
    List(const List& value);// copy constructor

    //Modification methods
    void Insert(const int);

    // User ACCESS methods
    void display(void) const;

private:
    int size;// MAX size of the array          
    int count;// current number of elements in the dynamic array

protected:
    int *intptr;// Our int pointer
};
list.cpp实现文件:

#include "list.h" // Include our Class defintion
#include <iostream>

using namespace std;

// CONSTRUCTOR
List::List() {
    size = 2; // initial size of array
    count = 0;
    intptr = new int[size]; // Start off 2 integers sized array
}
// DECONSTRUCTOR
List::~List() {
    delete[] intptr; // free allocated memory
}

// Copy constructor

List::List(const List& value) {
    size = value.size;
    cout << "Copy con size : " << size << endl;
    count = value.count;

    cout << "Compy count : " << count << endl;
    if (count < size) {
        intptr = new int[size]; // Allocate new data
    } else {
        intptr = new int[size + 2]; // Allocate new data
    }

    for (int index = 0; index < count; index++) {
        intptr[index] = value.intptr[index];
    }

    size = size + 2;
    delete[] intptr;
    intptr = value.intptr;
}

void List::Insert(const int value) {
    // do we have room?
    if (count < size) {
        intptr[count] = value;
    } else { // if not we need to add more elements to array
        intptr[count] = value; // DEEP copy invoked with copy constructor
    }

    cout << "SIZE: " << size << endl;
    cout << "COUNT" << count << endl;
    count++; // Increase items added in array
}

void List::display() const {
    for (int i = 0; i < count; i++)
        cout << intptr[i] << endl;
}
#包括“list.h”//包括我们的课程定义
#包括
使用名称空间std;
//建造师
列表::列表(){
size=2;//数组的初始大小
计数=0;
intptr=new int[size];//从2个整数大小的数组开始
}
//解构器
列表::~List(){
delete[]intptr;//释放分配的内存
}
//复制构造函数
列表::列表(常量列表和值){
大小=value.size;

不能使用std::vector。它已经完成了所有这一切,并且比您的代码更安全、更快。

您的
List::Insert(const int value)
方法根本不调用
列表
复制构造函数,它只在
intptr
数组内部写入。当
计数
大于
大小
时,您在数组外部写入,这就是出现错误的原因


您应该将在复制构造函数中执行的操作直接移动到
Insert
方法。

您没有正确管理数组,尤其是在
Insert()
方法中。请尝试以下操作:

#include "list.h" // Include our Class defintion 
#include <iostream> 

// CONSTRUCTOR 
List::List()  
{ 
    intptr = new int[2];
    size = 2;
    count = 0; 

    std::cout << "Initial size : " << size << " count : " << count << std::endl; 
} 

// DECONSTRUCTOR 
List::~List() 
{ 
    delete [] intptr; // free allocated memory 
} 

// Copy constructor 
List::List(const List& value) 
{ 
    intptr = new int[value.size]; // Allocate new data 
    size = value.size; 
    count = value.count; 

    for(int index = 0; index < count; ++index) 
        intptr[index] = value.intptr[index]; 

    std::cout << "Copy size : " << size << " count : " << count << std::endl; 
} 

void List::Insert(const int value) 
{ 
    if (count == size)
    { 
        int *newintptr = new int[size+2];

        for(int index = 0; index < size; ++index) 
            newintptr[index] = intptr[index]; 

        delete[] intptr;
        intptr = newintptr;
        size += 2;
    }

    intptr[count] = value; 
    ++count;

    std::cout << "New size : " << size << " count : " << count << std::endl; 
} 

void List::display() const 
{ 
    for(int i = 0; i < count; i++) 
        std::cout << intptr[i] << std::endl; 
} 
更进一步,如果您想继续使用自定义的
列表
类,请至少在其中使用
std::vector

#include <vector>

class List  
{  
public:  
    //  DEFAULT Constructor  
    List();  

    //Modification methods  
    void Insert(const int);  

    // User ACCESS methods  
    void display(void) const;  

protected:  
    std::vector<int> intvec;
};
#包括
班级名单
{  
公众:
//默认构造函数
List();
//修改方法
无效插入(常量int);
//用户访问方法
无效显示(void)常量;
受保护的:
std::vector intvec;
};

#包括“list.h”//包括我们的课程定义
#包括
//建造师
列表::列表()
{ 
国际储备(2);

复制构造函数的原因是从现有对象创建一个新对象,但是看看你复制的构造函数,你在那里做什么

/* initialize my size and count, from value */
size = value.size;
count = value.count;

/* Check count and size */
if( count == size ) /* if other is full */
    size += 2;

/* copy content from value.intptr into this->intptr */
//if (count < size)
//    intptr = new int[size]; // Allocate new data
//else
//    intptr = new int[size + 2]; // Allocate new data
intptr = new int[size];  /* Allocate my buffer */

/* It's better to use std::copy in place of a hand written loop */
//for(int index = 0; index < count; index++)
//    intptr[index] = value.intptr[index];
std::copy( value.intptr, value.intptr + value.count, intptr );

/* why you increase your size here?? shouldn't this indicate size of intptr? */
//size = size + 2;

/* After creating a new buffer and putting data into it, you destroy the buffer
   and set your buffer equal to buffer of value? why? if value destroyed it will
   destroy its intptr and your intptr point to a deleted memory
*/
//delete [] intptr;
// intptr = value.intptr;

索引
int*
并分配给结果如何调用类的副本构造函数?您声称正在执行深度副本,但似乎没有。您希望如何获取列表的副本?您是否考虑过只更新正在使用的数组的大小,而不依赖副本构造函数构造函数?为什么不使用
std::vector
std::list
类而不是编写自己的类?这两个类都是动态增长的列表。
std::vector是一个数组,很像您的类,而
std::list`是树节点的链接列表。虽然它是自动调用的,但复制构造函数将自动调用ed,如果你曾经做过拷贝,你就没有。你创建了一个实例,然后多次调用insert。这是一种家庭作业吗?我想不出你为什么会想正常地做这件事,除非你试图比std::vector更好地控制内存,因为每次需要更多的内存时,它的大小都会翻一番……呵呵但是,复制构造函数会被调用吗?如果你复制了对象的一个实例,复制构造函数会被调用。换句话说,如果你有两个列表实例,a和b,并且说a=b。尝试添加
列表myotherlist(mylist)
在你的main中,你会看到所有的
cout
出现:)看起来我试图用复制构造函数做的太多了,它的效果比我想象的要差,谢谢我在研究你的代码now@user978563不,我认为你很幸运,你学得很快:)
#include <iostream>            
#include <vector>
#include <algorithm>

void displayValue(int value)
{
    std::cout << value << std::endl; 
}

int main()            
{            
    std::vector<int> mylist;            

    mylist.push_back(5);            
    mylist.push_back(6);            
    mylist.push_back(2);            
    mylist.push_back(8);            
    mylist.push_back(4);            
    mylist.push_back(5);            
    mylist.push_back(9);            
    mylist.push_back(8);            
    mylist.push_back(5);            
    mylist.push_back(9);            
    mylist.push_back(8);            
    mylist.push_back(5);            
    mylist.push_back(9);            
    mylist.push_back(8);            
    mylist.push_back(5);            
    mylist.push_back(9);            

    std::for_each(mylist.begin(), myList.end(), displayValue);
    system("PAUSE");            

    std::vector<int> myList2(myList);

    std::for_each(mylist2.begin(), myList2.end(), displayValue);
    system("PAUSE");            

    return 0;            
 }       
#include <vector>

class List  
{  
public:  
    //  DEFAULT Constructor  
    List();  

    //Modification methods  
    void Insert(const int);  

    // User ACCESS methods  
    void display(void) const;  

protected:  
    std::vector<int> intvec;
};
#include "list.h" // Include our Class defintion 
#include <iostream> 

// CONSTRUCTOR 
List::List()  
{ 
    intvec.reserve(2);
    std::cout << "Initial size : " << intvec.capacity() << " count : " << intvec.size() << std::endl; 
} 

// Copy constructor 
List::List(const List& value) 
{ 
    intvec = value.intvec;
    std::cout << "Copy size : " << invec.capacity() << " count : " << intvec.size() << std::endl; 
} 

void List::Insert(const int value) 
{ 
    intvec.push_back(value); 
    std::cout << "New size : " << intvec.capacity() << " count : " << intvec.size() << std::endl; 
} 

void List::display() const 
{ 
    for(std::vector<int>::const_iterator iter = intvec.begin(), end = intvec.end(); iter != end; ++iter) 
        std::cout << *iter << std::endl; 
} 
#include <iostream> 
#include "list.h" 

int main() 
{ 
    List mylist; 

    mylist.Insert(5); 
    mylist.Insert(6); 
    mylist.Insert(2); 
    mylist.Insert(8); 
    mylist.Insert(4); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 

    mylist.display(); 
    system("PAUSE"); 

    List mylist2(myList); // copy construct a new list

    mylist2.display(); 
    system("PAUSE"); 

    return 0; 
} 
/* initialize my size and count, from value */
size = value.size;
count = value.count;

/* Check count and size */
if( count == size ) /* if other is full */
    size += 2;

/* copy content from value.intptr into this->intptr */
//if (count < size)
//    intptr = new int[size]; // Allocate new data
//else
//    intptr = new int[size + 2]; // Allocate new data
intptr = new int[size];  /* Allocate my buffer */

/* It's better to use std::copy in place of a hand written loop */
//for(int index = 0; index < count; index++)
//    intptr[index] = value.intptr[index];
std::copy( value.intptr, value.intptr + value.count, intptr );

/* why you increase your size here?? shouldn't this indicate size of intptr? */
//size = size + 2;

/* After creating a new buffer and putting data into it, you destroy the buffer
   and set your buffer equal to buffer of value? why? if value destroyed it will
   destroy its intptr and your intptr point to a deleted memory
*/
//delete [] intptr;
// intptr = value.intptr;
if(count < size) // do we have room?
{
    intptr[count] = value;
}
else // if not we need to add more elements to array
{
    /* As you already checked you do not have enough room to insert data to intptr
       so why you do it here? shouldn't you first allocate a new buffer and then
       copy data to it?
       In comment you say DEEP copy with copy-constructor, which copy constructor
       you expect to called here? you are assigning an int to another int, so where
       is copy constructor?
     */
    // intptr[count] = value; // DEEP copy invoked with copy constructor
    int* tmp = new int[size + 2];
    std::copy( intptr, intptr + size, tmp );
    delete[] intptr;
    intptr = tmp;
    size += 2;
    intptr[count] = value;
}

count++; // Increase items added in array