1、运行程序时,出现这个error C2600: 'Point::Point' : cannot define a compiler-generated special member function (must be declared in the class first)错误,感觉没什么问题,仔细一看才发现,是我错了。
2、我以为,不带参数的构造函数可以不用声明就直接在类外定义,没想到也需要在类外定义。
错误代码如下:
2_2_1.h
#ifndef _2_2_1_H_
#define _2_2_1_H_
class Point
{
private:
int x;
int y;
public:
void output();
};
#endif
2_2_1.cpp
#include"iostream.h"
#include"2_2_1.h"
void Point::output()
{
cout<<"x:"<<x<<" y:"<<y<<endl;
}
void Point::Initial()
{
x = 5 ;
y = 7 ;
}
Point::Point()
{
x = 6;
y = 6;
}
void main()
{
Point pt;
pt.output(); //构造函数的效果和Initial函数的效果是一样的
pt.Initial(); //类似于自己买零件,再用自己买的零件组装电脑。
pt.output(); //输出5和7
}
正确代码如下:
2_2_1.h
#ifndef _2_2_1_H_
#define _2_2_1_H_
class Point
{
private:
int x;
int y;
public:
void output();
Point();<span style="white-space:pre"> </span>//错误代码就少了这一句
};
#endif
2_2_1.cpp
#include"iostream.h"
#include"2_2_1.h"
void Point::output()
{
cout<<"x:"<<x<<" y:"<<y<<endl;
}
void Point::Initial()
{
x = 5 ;
y = 7 ;
}
Point::Point()
{
x = 6;
y = 6;
}
void main()
{
Point pt;
pt.output(); //构造函数的效果和Initial函数的效果是一样的
pt.Initial(); //类似于自己买零件,再用自己买的零件组装电脑。
pt.output(); //输出5和7
}