几行代码。编译没错,执行出错。大家帮看一上。多谢
几行代码。编译没错,执行出错。大家帮看一下。谢谢~
刚刚学到构造函数,遇到小问题,请教一下。
编译没错。但是执行出错。
#include <iostream>
using namespace std;
class car
{
public:
car();
int output();
private:
int color;
int size;
};
int car::output()
{
return color;
};
int main()
{
car car1;
cout<<car1.output();
return 0;
}
------解决方案--------------------
你有定义构造函数吗?声明了,就是要定义的。不声明,能自动生成
------解决方案--------------------
刚刚学到构造函数,遇到小问题,请教一下。
编译没错。但是执行出错。
#include <iostream>
using namespace std;
class car
{
public:
car();
int output();
private:
int color;
int size;
};
int car::output()
{
return color;
};
int main()
{
car car1;
cout<<car1.output();
return 0;
}
------解决方案--------------------
你有定义构造函数吗?声明了,就是要定义的。不声明,能自动生成
------解决方案--------------------
- C/C++ code
#include <iostream> using namespace std; class car { public: car(); int output(); private: int color; int size; }; car::car() { color = 0; size = 0; } int car::output() { return color; }; int main() { car car1; cout<<car1.output(); return 0; }
------解决方案--------------------
必须是错,基本类型,是不会自动初始化的,使用未初始化的变量,将导致不可预知的运行时错误!
------解决方案--------------------
car();构造函数没有定义~
------解决方案--------------------
- C/C++ code
#include "stdafx.h" #include <iostream> using namespace std; class car { public: car():color(0), size(0) {}; int output(); private: int color; int size; }; int car::output() { return color; }; int main() { car car1; cout<<car1.output(); return 0; }
------解决方案--------------------
建议详细了解编译编辑运行三个过程 。