注册 登录
编程论坛 C++教室

新手,求为什么?

zxstrom 发布于 2012-11-09 18:42, 406 次点击
请看下面的程序:
   class Box
        {
        public:
               
               Box(int,int,int,int);
                ~Box();
                void Input();
                int volume();
               
        private:
            int length;
            int width;
            int height;
            int num;
        };
Box::Box(int l,int w,int h,int n)
{
    length=l;
    width=w;
    height=h;
    num=n;
}

void Box::Input()
{
    cout<<"请输入有效数值:"<<endl;
    cin>>length
        >>width
        >>height
        >>num;
}        

Box::~Box()
{
    cout<<"第"<<num<<"析构函数被调用"<<endl;
}


int Box::volume()
{
    return length*width*height;
}

void main()
{
    int i;
    Box b[4];
    for( i=0;i<4;i++)
    {
      b[i].Input();
      cout<<"The volume is :"<<b[i].volume()<<endl;
    }
        

}
我的意图是想自己输入数值,但是现在程序编译错误,我不知道为什么,按道理说应该不会错啊,期待高手能指点迷津。
6 回复
#2
小小小火柴2012-11-09 19:16
  没头文件。
#3
小小小火柴2012-11-09 19:25
#include<iostream>
using namespace std;
class Box
        {
        public:
               
               Box()
               {};
                ~Box();
                void Input();
                int volume();
               
        private:
            int length;
            int width;
            int height;
            int num;
        };
int  Box::volume()   
{
     return length*width*height;
}
               
void Box::Input()
{
    cout<<"请输入有效数值:"<<endl;
    cin>>length
        >>width
        >>height
        >>num;
}        

Box::~Box()
{
    cout<<"第"<<num<<"析构函数被调用"<<endl;
}
void main()
{
    int i;
    Box *p=new Box[4];
    for( i=0;i<4;i++)
    {
      p[i].Input();
      cout<<"The volume is :"<<p[i].volume()<<endl;
    }
        

}
#4
青春无限2012-11-09 19:36
头文件没有
#5
zxd5432012-11-10 01:24
你的程序少一个系统默认的构造函数
class Box
        {
        public:
               
               Box(int,int,int,int);
               Box(){};//你自定义了一个构造函数 就必须写上这个
                ~Box();
                void Input();
                int volume();
               
        private:
            int length;
            int width;
            int height;
            int num;
        };
#6
天剑山2012-11-10 14:28
没有头文件,没有命名空间,没有默认构造函数......完全是一些基础问题,建议楼主先好好看看书再说
#7
zxstrom2012-12-15 12:18
回复 6楼 天剑山
汗,你说的肯定是有的了,我发帖,不可能不知道的?
1