二维数组类关于深拷贝

wukele 2006-11-16 07:21:17
class Arrary
{public:
Arrary(int m=5,int n=6,int value=0)
{
arrary=new int*[m];
for(int i=0;i<m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
*(*(arrary+i)+j)=value;

}
Arrary( Arrary &other)
{this->~Arrary();
printf("copy");
for(int i=0;i<other.m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<other.m;i++)
for(int j=0;j<other.n;j++)
{ *(*(arrary+i)+j)=other.arrary[i][j];
printf("%d",other.arrary[i][j]); }
m=other.m;
n=other.n;
}
~Arrary()
{for(int i=0;i<m;i++)
delete[] arrary[i];
delete[] arrary;

}
int GetValue(int m,int n)
{if( m<0 || m>this->m || n<0 || n>this->n)
{printf("访问不存在的元素!");
return 0;}
else
{return arrary[m][n];}

}
int SetValue(int m,int n)
{}

private:
int m,n;
int **arrary;
};
using namespace std;
void test(Arrary a)
{cout<<"test:"<<a.GetValue(4,4)<<endl;}
int main(){
Arrary a(5,5,4),b;
b=a;
//test(a);
cout<<b.GetValue(4,4);
system("pause");
return 0;
}
==================
我写的深拷贝函数有错吗?为什么运行的时候会出错啊。。
...全文
323 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2006-11-17
  • 打赏
  • 举报
回复
建议使用vector<vector<> >或者boost::multiarray。
要自己写,也最好参考它们的源码。
OOPhaisky 2006-11-16
  • 打赏
  • 举报
回复
this->m=5;
this->n=n;
------------------------------------------------------------------------------
汗!!
刚才看了半天,竟然没有看出来,我就觉得是m,n还有分配释放的问题,但是没看出来,自己也惭愧啊!!
wukele 2006-11-16
  • 打赏
  • 举报
回复
不好意思啊。。是我自己的疏忽造成运行错误的。。我少写代码
现在把正确地贴上来
#include<iostream>
class Arrary
{public:
Arrary(int m=5,int n=6,int value=0)
{
arrary=new int*[m];
for(int i=0;i<m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
*(*(arrary+i)+j)=value;
this->m=5;
this->n=n;

}
Arrary( Arrary &other)
{//this->~Arrary();
printf("copy%l",other.m);
arrary=new int*[other.m];
for(int i=0;i<other.m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<other.m;i++)
for(int j=0;j<other.n;j++)
{ *(*(arrary+i)+j)=other.arrary[i][j];
}
m=other.m;
n=other.n;

}
// Arrary& operator =( const Arrary &other )
// { printf("copytret%d",other.m);}
~Arrary()
{for(int i=0;i<m;i++)
delete[] arrary[i];
delete[] arrary;

}
int GetValue(int m,int n)
{if( m<0 || m>this->m || n<0 || n>this->n)
{printf("访问不存在的元素!");
return 0;}
else
{return arrary[m][n];}

}
int SetValue(int m,int n)
{}

private:
int m,n;
int **arrary;
};
using namespace std;
void test(Arrary a)
{cout<<"test:"<<a.GetValue(4,4)<<endl;}
int main(){
Arrary a(5,5,4),b;
// b=a;
//Arrary b=Arrary(a);
test(a);
cout<<b.GetValue(4,4);
system("pause");
return 0;
}
wukele 2006-11-16
  • 打赏
  • 举报
回复
把上面的拷贝函数改为这个
Arrary( Arrary &other)
{
printf("copy");
arrary=new int*[other.m];
for(int i=0;i<other.m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<other.m;i++)
for(int j=0;j<other.n;j++)
{ *(*(arrary+i)+j)=other.arrary[i][j];
printf("%d",other.arrary[i][j]); }
m=other.m;
n=other.n;
}
运行的时候还是会出错
wukele 2006-11-16
  • 打赏
  • 举报
回复
把上面的拷贝函数改为这个
Arrary( Arrary &other)
{
printf("copy");
arrary=new int*[other.m];
for(int i=0;i<other.m;i++)
{arrary[i]=new int[n];
}
for(int i=0;i<other.m;i++)
for(int j=0;j<other.n;j++)
{ *(*(arrary+i)+j)=other.arrary[i][j];
printf("%d",other.arrary[i][j]); }
m=other.m;
n=other.n;
}
OOPhaisky 2006-11-16
  • 打赏
  • 举报
回复
好像是析构函数导致的运行时错误......
wukele 2006-11-16
  • 打赏
  • 举报
回复
不好意思,我忘记把this- >~Arrary()注释掉了。。
把它注释了。。但是还会出错~~
a_b_c_abc2 2006-11-16
  • 打赏
  • 举报
回复
拷贝构造是用一个已存在的对象去构造一个不存在的对象 。

//this->~Arrary(); 对象都没建立,析构个屁啊
内容概要:本文系统研究了永磁同步电机(PMSM)的速度控制问题,重点探讨了矢量控制(FOC)技术的原理与实现方法,并基于Simulink搭建了完整的仿真模型进行验证。文章深入剖析了矢量控制的核心环节,包括Clarke变换与Park变换在内的坐标变换原理、磁场定向控制策略、双闭环(电流环与速度环)PI控制器的设计,以及空间矢量脉宽调制(SVPWM)技术的应用。通过仿真实验,验证了该控制策略在调节PMSM转速和转矩时具备良好的动态响应性能和稳态精度,为高性能电机驱动系统的研发提供了详实的理论依据和技术参考。; 适合人群:具备电机学、电力电子技术和自动控制理论基础的电气工程、自动化、机电一体化等相关专业的本科生、研究生、科研人员及从事电机驱动开发的工程技术人员。; 使用场景及目标:①深入学习和掌握永磁同步电机矢量控制的理论基础与关键技术;②利用Simulink进行电机控制系统的建模、仿真与算法验证,服务于课程设计、毕业设计或科研项目;③为工业领域中高性能伺服驱动器、新能源汽车电驱系统等产品的开发提供技术方案与仿真支持。; 阅读建议:建议读者结合提供的Simulink仿真模型,动手实践并调整控制器参数(如PI参数),通过对比不同参数下的系统响应曲线,深刻理解各控制环节的作用和系统动态性能的优化方法。

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧