基本概念有关问题,请求高人给予详细解释,高分回报
基本概念问题,请求高人给予详细解释,高分回报。
类声明:
class CMyLocation//第一个类
{
public:
CMyLocation();
CMyLocation(int x, int y);
~CMyLocation();
static float MyPointDistance(CMyLocation& firstCMyLocation, CMyLocation& secondCMyLocation);
private:
int iX;
int iY;
};
class CMyLine//第二个类
{
public:
CMyLine(int x0, int y0, int x1, int y1);
~CMyLine();
float MyLineLength();
private:
int iX0;
int iX1;
int iY0;
int iY1;
CMyLocation iStartpoint;
CMyLocation iEndpoint;//声明了前一个类类型的成员变量
};
类定义:
CMyLocation::CMyLocation()//默认构造函数
{
iX = 0;
iY = 0;
}
CMyLocation::CMyLocation(int x, int y)//带参数的构造函数
{
iX = x;
iY = y;
}
CMyLocation::~CMyLocation(){};//析构函数
float CMyLocation::MyPointDistance(CMyLocation& firstCMyLocation, CMyLocation& secondCMyLocation)
{
return sqrt(float((firstCMyLocation.iX - secondCMyLocation.iX) +
(firstCMyLocation.iY - secondCMyLocation.iY)));
}
CMyLine::CMyLine(int x0, int y0, int x1, int y1):iStartpoint(x0, y0),iEndpoint(x1,y1)
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
}
CMyLine::~CMyLine(){};
float CMyLine::MyLineLength()//如果需要使用其它类的成员函数,而没有继承,是不是那个函数必须是静态函数,有无其它方法?
{
return CMyLocation::MyPointDistance(iStartpoint, iEndpoint);
}
问题:
1、如果我使用下面这个方式构造类对象,那么就会出现error C2064: 项不会计算为接受 2 个参数的函数,这是为什么?
CMyLine::CMyLine(int x0, int y0, int x1, int y1
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
iStartpoint(x0, y0);
iEndpoint(x1,y1);
}
2、如果我不定义CMyLocation的默认构造函数,只定义带参数的构造函数,就会产生错误,classtest error LNK2019: 无法解析的外部符号 "public: __thiscall CMyLocation::CMyLocation(void) " (??0CMyLocation@@QAE@XZ) ,该符号在函数 "public: __thiscall CMyCircle::CMyCircle(int,int,int) " (??0CMyCircle@@QAE@HHH@Z) 中被引用
我在CMyLine里面声明了类型为CMyLocation的成员变量,这里只是声明,并没有产生什么动作,怎么会调用构造函数?在CMyLine的构造函数里面构造CMyLocation类型成员,使用了带参数的构造函数,怎么还要默认的?
这是两个毫不相干的类之间的成员定义和使用的问题,我在使用一些api的时候没有遇到过这样的现象。请求帮助。
谢谢。
------解决方案--------------------
2.由于你声明的是一个实体(就不是指针)所以一旦声明并且主类被实体化,这个也被实体化,换句话说就是要开始构造,然而你删除了空构造函数(声明中要求一个默认空构造函数来构造类)所以会出现LINKerror找不到默认空构造函数~
------解决方案--------------------
float CMyLine::MyLineLength()//如果需要使用其它类的成员函数,而没有继承,是不是那个函数必须是静态函数,有无其它方法?
{
return CMyLocation::MyPointDistance(iStartpoint, iEndpoint);
}
----------------
可以不是静态的,比如你把 MyPointDistance 改成不是静态的
1)
CMyLine::CMyLine(int x0, int y0, int x1, int y1
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
iStartpoint(x0, y0);
iEndpoint(x1,y1);
}
改成
CMyLine::CMyLine(int x0, int y0, int x1, int y1
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
iStartpoint = CMyLocation(x0, y0);
iEndpoint = CMyLocation(x1,y1);
类声明:
class CMyLocation//第一个类
{
public:
CMyLocation();
CMyLocation(int x, int y);
~CMyLocation();
static float MyPointDistance(CMyLocation& firstCMyLocation, CMyLocation& secondCMyLocation);
private:
int iX;
int iY;
};
class CMyLine//第二个类
{
public:
CMyLine(int x0, int y0, int x1, int y1);
~CMyLine();
float MyLineLength();
private:
int iX0;
int iX1;
int iY0;
int iY1;
CMyLocation iStartpoint;
CMyLocation iEndpoint;//声明了前一个类类型的成员变量
};
类定义:
CMyLocation::CMyLocation()//默认构造函数
{
iX = 0;
iY = 0;
}
CMyLocation::CMyLocation(int x, int y)//带参数的构造函数
{
iX = x;
iY = y;
}
CMyLocation::~CMyLocation(){};//析构函数
float CMyLocation::MyPointDistance(CMyLocation& firstCMyLocation, CMyLocation& secondCMyLocation)
{
return sqrt(float((firstCMyLocation.iX - secondCMyLocation.iX) +
(firstCMyLocation.iY - secondCMyLocation.iY)));
}
CMyLine::CMyLine(int x0, int y0, int x1, int y1):iStartpoint(x0, y0),iEndpoint(x1,y1)
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
}
CMyLine::~CMyLine(){};
float CMyLine::MyLineLength()//如果需要使用其它类的成员函数,而没有继承,是不是那个函数必须是静态函数,有无其它方法?
{
return CMyLocation::MyPointDistance(iStartpoint, iEndpoint);
}
问题:
1、如果我使用下面这个方式构造类对象,那么就会出现error C2064: 项不会计算为接受 2 个参数的函数,这是为什么?
CMyLine::CMyLine(int x0, int y0, int x1, int y1
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
iStartpoint(x0, y0);
iEndpoint(x1,y1);
}
2、如果我不定义CMyLocation的默认构造函数,只定义带参数的构造函数,就会产生错误,classtest error LNK2019: 无法解析的外部符号 "public: __thiscall CMyLocation::CMyLocation(void) " (??0CMyLocation@@QAE@XZ) ,该符号在函数 "public: __thiscall CMyCircle::CMyCircle(int,int,int) " (??0CMyCircle@@QAE@HHH@Z) 中被引用
我在CMyLine里面声明了类型为CMyLocation的成员变量,这里只是声明,并没有产生什么动作,怎么会调用构造函数?在CMyLine的构造函数里面构造CMyLocation类型成员,使用了带参数的构造函数,怎么还要默认的?
这是两个毫不相干的类之间的成员定义和使用的问题,我在使用一些api的时候没有遇到过这样的现象。请求帮助。
谢谢。
------解决方案--------------------
2.由于你声明的是一个实体(就不是指针)所以一旦声明并且主类被实体化,这个也被实体化,换句话说就是要开始构造,然而你删除了空构造函数(声明中要求一个默认空构造函数来构造类)所以会出现LINKerror找不到默认空构造函数~
------解决方案--------------------
float CMyLine::MyLineLength()//如果需要使用其它类的成员函数,而没有继承,是不是那个函数必须是静态函数,有无其它方法?
{
return CMyLocation::MyPointDistance(iStartpoint, iEndpoint);
}
----------------
可以不是静态的,比如你把 MyPointDistance 改成不是静态的
1)
CMyLine::CMyLine(int x0, int y0, int x1, int y1
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
iStartpoint(x0, y0);
iEndpoint(x1,y1);
}
改成
CMyLine::CMyLine(int x0, int y0, int x1, int y1
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
iStartpoint = CMyLocation(x0, y0);
iEndpoint = CMyLocation(x1,y1);