问答首页 程序设计C/C++  
 
此C++代码哪里有错?

#include <iostream>
using namespace std;
class rectan
{
private:
 int length,width;
public:
 rectan(int m,int n)
 {
  length=m;
  width=n;
 }
 show()
 {
  return length*width;
 }
};
int main()
{

 rectan a;
 a.rectan(3,5);
 cout<<a.show()<<endl;
 return 0;
}  
最佳答案
rectan类没有提供无参构造函数,所以在定义a时必须提供初始化参数,修改如下:

#include <iostream>
using namespace std;
class rectan
{
private:
 int length,width;
public:
 rectan(int m,int n)
 {
  length=m;
  width=n;
 }
 show()
 {
  return length*width;
 }
};
int main()
{

 rectan a(3,5); //注意这里
    //注意这里
 cout<<a.show()<<endl;
 return 0;
}