#include <iostream>
#include <afxtempl.h>
using namespace std;
class CPoint
{
public:
CPoint(int x1,int y1)
{
x=x1;
y=y1;
}
~CPoint()
{
;
}
//private:
int x;
int y;
};
int main()
{
CArray<CPoint,CPoint &> arrPoint;
int i;
//赋值
for(i=0; i<10; i++)
arrPoint.Add(CPoint(i,i));
TRACE("ArrPoint Size:%d,UpperBound:%d\n",
arrPoint.GetSize(),
arrPoint.GetUpperBound());
//如果事先知道要存放多少个元素,可以调用SetSize();
arrPoint.SetSize(20);
TRACE("ArrPoint Size:%d,UpperBound:%d\n",
arrPoint.GetSize(),
arrPoint.GetUpperBound());
CPoint pt1=arrPoint.GetAt(3); //以函数调用方式返回数组元素的拷贝
CPoint &ptx=arrPoint.GetAt(3); //以函数调用方式返回数组元素的拷贝(谨慎!!!)
CPoint &pt2=arrPoint.ElementAt(3); //以调用重载数组运算符的方式获取元素的引用
CPoint &pt3=arrPoint[3]; //以调用重载数组运算符的方式获取元素的引用
//遍历数组
//for(i=0; i<arrPoint.GetSize(); i++) //一般来说,如果数组比较大,下面两句比只用这一句效率更高。
//GetSize()返回的是元素个数,并不是缓冲区的大小!!!!!!
//缓冲区能存放元素的个数大于等于GetSize()
int iSize=arrPoint.GetSize();
for(i=0; i<iSize; i++)
{
CPoint pt3=arrPoint[i];
TRACE("Point%d x:%d y:%d\n",i,pt3.x,pt3.y);
}
//CArray虽然支持元素的插入和删除,但是因为存在后续元素的块拷贝,
//所以效率比CList低,如果有频繁的插入或删除操作,应该用CList,而不宜用CArray
arrPoint.RemoveAt(3);
arrPoint.RemoveAll(); //清空数组,不一定要调用。
return 0;
}
10 个解决方案
#1
把CPoint的拷贝构造函数,重载操作符"="的函数都补上看看
#2
补上还是提示相同的错误
#3
CPoint 根本没有定义默认构造函数,这怎么行呢?
#4
CPoint():x(0),y(0)
{
}
#5
加上默认构造函数则出现如下错误:
error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
error LNK2005: "public: __thiscall CPoint::CPoint(void)" (??0CPoint@@QAE@XZ) already defined in main.obj
LNK2005: "public: __thiscall CPoint::CPoint(int,int)" (??0CPoint@@QAE@HH@Z) already defined in main.obj
LNK2001: unresolved external symbol __endthreadex
LNK2001: unresolved external symbol __beginthreadex
error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
error LNK2005: "public: __thiscall CPoint::CPoint(void)" (??0CPoint@@QAE@XZ) already defined in main.obj
LNK2005: "public: __thiscall CPoint::CPoint(int,int)" (??0CPoint@@QAE@HH@Z) already defined in main.obj
LNK2001: unresolved external symbol __endthreadex
LNK2001: unresolved external symbol __beginthreadex
#6
o,应该是你定义的类类型和库相同,换一个名字吧
#7
改后代码如下:还是提示有错误
#include <iostream>
#include <afxtempl.h>
using namespace std;
class CMyPoint
{
public:
CMyPoint()
{
x=0;
y=0;
}
CMyPoint(int x1,int y1)
{
x=x1;
y=y1;
}
CMyPoint(const CMyPoint &obj)
{
x=obj.x;
y=obj.y;
}
CMyPoint operator=(CMyPoint obj)
{
x=obj.x;
y=obj.y;
return *this;
}
~CMyPoint()
{
;
}
//private:
int x;
int y;
};
int main()
{
CArray<CMyPoint,CMyPoint &> arrPoint;
int i;
//赋值
for(i=0; i<10; i++)
arrPoint.Add(CMyPoint(i,i));
TRACE("ArrPoint Size:%d,UpperBound:%d\n",
arrPoint.GetSize(),
arrPoint.GetUpperBound());
//如果事先知道要存放多少个元素,可以调用SetSize();
arrPoint.SetSize(20);
TRACE("ArrPoint Size:%d,UpperBound:%d\n",
arrPoint.GetSize(),
arrPoint.GetUpperBound());
CMyPoint pt1=arrPoint.GetAt(3); //以函数调用方式返回数组元素的拷贝
CMyPoint &ptx=arrPoint.GetAt(3); //以函数调用方式返回数组元素的拷贝(谨慎!!!)
CMyPoint &pt2=arrPoint.ElementAt(3); //以调用重载数组运算符的方式获取元素的引用
CMyPoint &pt3=arrPoint[3]; //以调用重载数组运算符的方式获取元素的引用
//遍历数组
//for(i=0; i<arrPoint.GetSize(); i++) //一般来说,如果数组比较大,下面两句比只用这一句效率更高。
//GetSize()返回的是元素个数,并不是缓冲区的大小!!!!!!
//缓冲区能存放元素的个数大于等于GetSize()
int iSize=arrPoint.GetSize();
for(i=0; i<iSize; i++)
{
CMyPoint pt3=arrPoint[i];
TRACE("Point%d x:%d y:%d\n",i,pt3.x,pt3.y);
}
//CArray虽然支持元素的插入和删除,但是因为存在后续元素的块拷贝,
//所以效率比CList低,如果有频繁的插入或删除操作,应该用CList,而不宜用CArray
arrPoint.RemoveAt(3);
arrPoint.RemoveAll(); //清空数组,不一定要调用。
return 0;
}
error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
#8
什么编译器?
2003以上的:
在项目->属性->链接器->命令行->附加选项中加 /force
2003以上的:
在项目->属性->链接器->命令行->附加选项中加 /force
#9
VC6.0
#10
http://support.microsoft.com/kb/148652/zh-cn
#1
把CPoint的拷贝构造函数,重载操作符"="的函数都补上看看
#2
补上还是提示相同的错误
#3
CPoint 根本没有定义默认构造函数,这怎么行呢?
#4
CPoint():x(0),y(0)
{
}
#5
加上默认构造函数则出现如下错误:
error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
error LNK2005: "public: __thiscall CPoint::CPoint(void)" (??0CPoint@@QAE@XZ) already defined in main.obj
LNK2005: "public: __thiscall CPoint::CPoint(int,int)" (??0CPoint@@QAE@HH@Z) already defined in main.obj
LNK2001: unresolved external symbol __endthreadex
LNK2001: unresolved external symbol __beginthreadex
error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
error LNK2005: "public: __thiscall CPoint::CPoint(void)" (??0CPoint@@QAE@XZ) already defined in main.obj
LNK2005: "public: __thiscall CPoint::CPoint(int,int)" (??0CPoint@@QAE@HH@Z) already defined in main.obj
LNK2001: unresolved external symbol __endthreadex
LNK2001: unresolved external symbol __beginthreadex
#6
o,应该是你定义的类类型和库相同,换一个名字吧
#7
改后代码如下:还是提示有错误
#include <iostream>
#include <afxtempl.h>
using namespace std;
class CMyPoint
{
public:
CMyPoint()
{
x=0;
y=0;
}
CMyPoint(int x1,int y1)
{
x=x1;
y=y1;
}
CMyPoint(const CMyPoint &obj)
{
x=obj.x;
y=obj.y;
}
CMyPoint operator=(CMyPoint obj)
{
x=obj.x;
y=obj.y;
return *this;
}
~CMyPoint()
{
;
}
//private:
int x;
int y;
};
int main()
{
CArray<CMyPoint,CMyPoint &> arrPoint;
int i;
//赋值
for(i=0; i<10; i++)
arrPoint.Add(CMyPoint(i,i));
TRACE("ArrPoint Size:%d,UpperBound:%d\n",
arrPoint.GetSize(),
arrPoint.GetUpperBound());
//如果事先知道要存放多少个元素,可以调用SetSize();
arrPoint.SetSize(20);
TRACE("ArrPoint Size:%d,UpperBound:%d\n",
arrPoint.GetSize(),
arrPoint.GetUpperBound());
CMyPoint pt1=arrPoint.GetAt(3); //以函数调用方式返回数组元素的拷贝
CMyPoint &ptx=arrPoint.GetAt(3); //以函数调用方式返回数组元素的拷贝(谨慎!!!)
CMyPoint &pt2=arrPoint.ElementAt(3); //以调用重载数组运算符的方式获取元素的引用
CMyPoint &pt3=arrPoint[3]; //以调用重载数组运算符的方式获取元素的引用
//遍历数组
//for(i=0; i<arrPoint.GetSize(); i++) //一般来说,如果数组比较大,下面两句比只用这一句效率更高。
//GetSize()返回的是元素个数,并不是缓冲区的大小!!!!!!
//缓冲区能存放元素的个数大于等于GetSize()
int iSize=arrPoint.GetSize();
for(i=0; i<iSize; i++)
{
CMyPoint pt3=arrPoint[i];
TRACE("Point%d x:%d y:%d\n",i,pt3.x,pt3.y);
}
//CArray虽然支持元素的插入和删除,但是因为存在后续元素的块拷贝,
//所以效率比CList低,如果有频繁的插入或删除操作,应该用CList,而不宜用CArray
arrPoint.RemoveAt(3);
arrPoint.RemoveAll(); //清空数组,不一定要调用。
return 0;
}
error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
#8
什么编译器?
2003以上的:
在项目->属性->链接器->命令行->附加选项中加 /force
2003以上的:
在项目->属性->链接器->命令行->附加选项中加 /force
#9
VC6.0
#10
http://support.microsoft.com/kb/148652/zh-cn