这个程序运行会出错
这个程序在Dev-C++编译运行出错,原因在哪儿?/************************************************
* array_test -- Test the use of the array class*
************************************************/
#include <iostream>
#include <cstring>
/************************************************
* array -- Classic variable length array class.*
* *
* Member functions: *
* operator [] -- Return an item *
* in the array. *
************************************************/
class array {
protected:
// Size of the array
int size;
// The array data itself
int *data;
public:
// Constructor.
// Set the size of the array
// and create data
array(const int i_size):
size(i_size),
data(new int)
{
// Clear the data
memset(data, '\0',
size * sizeof(data));
}
// Destructor -- Return data to the heap
virtual ~array(void)
{
delete []data;
data = NULL;
}
// Copy constructor.
// Delete the old data and copy
array(const array &old_array)
{
delete []data;
data = new int;
memcpy(data, old_array.data,
size * sizeof(data));
}
// operator =.
// Delete the old data and copy
array & operator = (
const array &old_array)
{
if (this == &old_array)
return (*this);
delete []data;
data = new int;
memcpy(data, old_array.data,
size * sizeof(data));
return (*this);
}
public:
// Get a reference to an item in the array
int &operator [](const unsigned int item)
{
return data;
}
};
/**********************************************
* three_more_elements-- *
* Copy from_array to to_array and *
* put on three more elements. *
**********************************************/
void three_more_elements(
// Original array
array to_array,
// New array with modifications
const array &from_array
)
{
to_array = from_array;
to_array = 1;
to_array = 3;
to_array = 5;
}
int main()
{
array an_array(30);// Simple test array
an_array = 2; // Put in an element
// Put on a few more
three_more_elements(an_array, an_array);
return(0);
}
自己调试看看吧! 在VC++编译里是可以通过的,源代码没问题,那就是编译器的问题了,换个编译器楼主 红头发 发表于 2014-1-19 21:16 static/image/common/back.gif
在VC++编译里是可以通过的,源代码没问题,那就是编译器的问题了,换个编译器楼主
我当然知道vc++能够编译成功啦。。但真的没问题吗?拷贝构造函数那里。。。
页:
[1]