Re: [問題] 程式記憶體區段錯誤
基本上你要知道你類別內的方法
做了什麼事情
你在copy constructor裡面
你沒有賦予這個class 裡的成員的值
他在呼叫show的時候當然就爆炸啦~(因為裡面當是垃圾資料)
另外好像是打錯了吧 ptr -> ptrl....
恩下次多注意一下
: 下面程式跑了以後會有 程式記憶體區段錯誤
: 請問我這是哪裡出錯了呢,謝謝
: #include <iostream>
: #include <stdio.h>
: #include <string.h>
: using namespace std;
: class Caaa
: {
: private:
: int total, *ptr;
: public:
: Caaa(int num):total(num)
: {
: ptr = new int[num];
: for(int i=0;i<num;i++) ptr[i]=i;
: }
: void show()
: {
: for(int j=0;j<this->total;j++)
: cout << this->ptr[j] <<" " ;
: cout << endl;
: }
: Caaa(const Caaa &c)
: {
: // int *ptr1 = new int[c.total];
ptr = new int[c.total] ;
: for(int k=0;k<c.total;k++)
//ptr1[k]=c.ptr[k];
ptr[k] = c.ptr[k];
total = c.total
: }
: ~Caaa()
: { delete [] ptr;}
: };
: int main()
: {
: Caaa a(10);
: Caaa b(a);
: a.show();
: b.show();
: }
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 134.208.26.38
推
04/18 12:18, , 1F
04/18 12:18, 1F
→
04/18 13:52, , 2F
04/18 13:52, 2F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
1
3
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章