鱼C论坛

 找回密码
 立即注册
查看: 3028|回复: 1

这个程序运行会出错

[复制链接]
发表于 2014-1-19 11:36:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个程序在Dev-C++编译运行出错,原因在哪儿?
  1. /************************************************
  2. * array_test -- Test the use of the array class*
  3. ************************************************/
  4. #include <iostream>
  5. #include <cstring>
  6. /************************************************
  7. * array -- Classic variable length array class.*
  8. *                                              *
  9. * Member functions:                            *
  10. *      operator [] -- Return an item           *
  11. *              in the array.                   *
  12. ************************************************/
  13. class array {
  14.     protected:
  15.         // Size of the array
  16.         int size;
  17.         // The array data itself
  18.         int *data;
  19.     public:
  20.         // Constructor.
  21.         // Set the size of the array
  22.         // and create data
  23.         array(const int i_size):
  24.             size(i_size),
  25.             data(new int[size])
  26.         {
  27.             // Clear the data
  28.             memset(data, '\0',
  29.                     size * sizeof(data[0]));
  30.         }
  31.         // Destructor -- Return data to the heap
  32.         virtual ~array(void)
  33.         {
  34.             delete []data;
  35.             data = NULL;
  36.         }
  37.         // Copy constructor.
  38.         // Delete the old data and copy
  39.         array(const array &old_array)
  40.         {
  41.             delete []data;
  42.             data = new int[old_array.size];
  43.             memcpy(data, old_array.data,
  44.                     size * sizeof(data[0]));
  45.         }
  46.         // operator =.
  47.         // Delete the old data and copy
  48.         array & operator = (
  49.                 const array &old_array)
  50.         {
  51.                         if (this == &old_array)
  52.                    return (*this);
  53.             delete []data;
  54.             data = new int[old_array.size];
  55.             memcpy(data, old_array.data,
  56.                     size * sizeof(data[0]));
  57.             return (*this);
  58.         }
  59.     public:
  60.         // Get a reference to an item in the array
  61.         int &operator [](const unsigned int item)
  62.         {
  63.             return data[item];
  64.         }
  65. };
  66. /**********************************************
  67. * three_more_elements  --                    *
  68. *      Copy from_array to to_array and       *
  69. *      put on three more elements.           *
  70. **********************************************/
  71. void three_more_elements(
  72.     // Original array
  73.     array to_array,
  74.     // New array with modifications
  75.     const array &from_array
  76. )
  77. {
  78.     to_array = from_array;
  79.     to_array[10] = 1;
  80.     to_array[11] = 3;
  81.     to_array[11] = 5;
  82. }
  83. int main()
  84. {
  85.     array an_array(30);  // Simple test array
  86.     an_array[2] = 2;    // Put in an element
  87.     // Put on a few more
  88.     three_more_elements(an_array, an_array);
  89.     return(0);
  90. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-1-19 16:39:00 | 显示全部楼层
自己调试看看吧!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-1-19 21:16:17 | 显示全部楼层
在VC++编译里是可以通过的,源代码没问题,那就是编译器的问题了,换个编译器楼主
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-20 08:39:42 | 显示全部楼层

我当然知道vc++能够编译成功啦。。但真的没问题吗?拷贝构造函数那里。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-14 04:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表