// 洛辰.cpp : 定义控制台应用程序的入口点。
//
/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称:score.cpp
* 作 者:董慧
* 完成日期:2013年4月13日
* 版本号: v1.0
* 输入描述:略
* 问题描述:略
* 输出:
*/
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Box
{
public:
Box(int h,int w,int l):height(h),width(w),length(l){}
int volume( ){return height*width*length;};
private:
static int height; //静态的数据成员
int width;
int length;
};
int main()
{
Box b(2,3,4);
cout<<"volume is "<<b.volume()<<endl;
return 0;
}
//1>c:\documents and settings\administrator\桌面\洛辰\洛辰\洛辰.cpp(11): error C2438: “height”: 无法通过构造函数初始化静态类数据
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class Box{
public:
Box(int ,int );//定义构造函数时不能对height进行初始化
int volume(){return height*width*length;};
private:
static int height;//静态的数据成员
//这里将height定义成了静态的数据成员,所以上面定义构造函数时不能对height进行初始化
int width;
int length;
};
Box::Box(int w,int l){
width=w;
length=l;
}
int Box::height=2;//表示对Box类中的数据成员初始化
int main(){
Box b(3,4);
cout<<"volume is "<<b.volume()<<endl;
return 0;
}
改错
最新推荐文章于 2024-03-22 21:04:17 发布