修改c++程序的错误

来源:百度知道 编辑:UC知道 时间:2024/07/02 02:06:08
修改每个注释后的错误,使程序的输出结果为:There are 2 object(s)
// proj1.cpp
#include <iostream>
using namespace std;

class MyClass {
public:
// ERROR **********found**********
MyClass(int i = 0) value = i
{ count++; }
void Print()
{ cout << "There are " << count << " object(s)." << endl; }
private:
const int value;
static int count;
};
// ERROR **********found**********
static int MyClass::count = 0;
int main()
{
MyClass obj1, obj2;
// ERROR **********found**********
MyClass.Print();
return 0;
}
MyClass()
{ count++; }为非静态成员函数对静态成员函数的访问应该不能使用内联函数啊?!这里不是有错吗?

// proj1.cpp
#include <iostream>
using namespace std;

class MyClass {
public:
// ERROR **********found**********
MyClass(int i = 0) value = i /*value是个常变量,赋值有问题。 value=i这里有用吗?不明白这里写些什么。构造函数?函数体好像也没有value=i包括在里面呀。*/
{ count++; }
void Print()
{ cout << "There are " << count << " object(s)." << endl; }
private:
const int value;
static int count;
};
// ERROR **********found**********
static int MyClass::count = 0;//去掉static
int main()
{
MyClass obj1, obj2; //定义这个干啥?好像你的程序里没用到。
// ERROR **********found**********
MyClass.Print(); //Print()不是static函数,要用对象来调用,或者定义时定义为static函数。
return 0;
}

楼主的错误比较多,具体修改如下:

#include <iostream>
using namespace std;

class MyClass {
public:

MyClass()
{ count++; }
void Print()
{ cout << "There