struct结构有自己的拷贝构造功能吗?

liaozhicai 2005-06-12 01:52:17
error C2664: 'Add' : cannot convert parameter 1 from 'struct tagTrainStopTime' to 'struct tagTrainStopTime'
No copy constructor available for struct 'tagTrainStopTime'
typedef struct tagTrainStopTime
{
int x1;
int x2;
} P;
CArray<P,P> a;
P p1;
p1.x1=1;
p1.x2=2;
a.Add(p1);
请问有没有简单的办法可以解决这个问题(不是重新编写成类,并为它定义拷贝构造函数)?
...全文
442 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
狂放之歌 2005-06-12
  • 打赏
  • 举报
回复
标准C++中 struct 和 class的 区别 仅仅是 默认 public 和private
liaozhicai 2005-06-12
  • 打赏
  • 举报
回复
我重载了=操作符号,暂时通过了编译
void operator=(tagTrainStopTime& t)
{
nStopAtCount=t.nStopAtCount;
nTrainIdxU=t.nTrainIdxU;
nTrainIdxD=t.nTrainIdxD;
arrStopTimesX.Copy(t.arrStopTimesX);
arrStopTimes.Copy(t.arrStopTimes);

}
liaozhicai 2005-06-12
  • 打赏
  • 举报
回复
如果是重写copyelement函数,是在哪重写啊?在afxtempl.h里多加一个定义吗?
liaozhicai 2005-06-12
  • 打赏
  • 举报
回复
我头都大了。哪位仁兄可不可以给我一个具体的思路。
c:\program files\microsoft visual studio\vc98\mfc\include\afxtempl.h(443) : error C2582: 'tagTrainStopTime' : 'operator =' function is unavailable
我在结构里自己写了两个构造函数
typedef struct tagTrainStopTime//列车在各站的停站时间
{
int nStopAtCount;//经停车站
//CString szTrainIdx;//车次
int nTrainIdxU;//程序内部使用上行车次,从1,3,5。。。计
int nTrainIdxD;
CArray<float,float> arrStopTimesX;//存储X坐标即时间
CArray<CString,CString> arrStopTimes;
tagTrainStopTime()
{
nStopAtCount=0;
nTrainIdxU=0;
nTrainIdxD=0;
arrStopTimesX.SetSize(0);
arrStopTimes.SetSize(0);

};
tagTrainStopTime(tagTrainStopTime& t)
{
nStopAtCount=t.nStopAtCount;
nTrainIdxU=t.nTrainIdxU;
nTrainIdxD=t.nTrainIdxD;
arrStopTimesX.Copy(t.arrStopTimesX);
arrStopTimes.Copy(t.arrStopTimes);
};
}TRAINSTOPTIMES;

结构里先使用了CArray类。是不是有循环调用的嫌疑?
truewill 2005-06-12
  • 打赏
  • 举报
回复
呵呵,你的情况似乎是应该重写CopyElements
oyljerry 2005-06-12
  • 打赏
  • 举报
回复
自己定义拷贝构造函数
truewill 2005-06-12
  • 打赏
  • 举报
回复
看看msdn,那里有说
重写ConstructElements()函数

void AFXAPI ConstructElements<P>( P* pElements, int nCount )
{
......
}

ps:其实struct跟class一样,也可以加入构造函数

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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