读书人

c++程序修改不知道哪错了。该如何处

发布时间: 2013-04-20 19:43:01 作者: rapoo

c++程序修改,不知道哪错了。
#include <iostream>
#include <string>
using namespace std;
class Part
{
public:
Part(char *pname="no name")
{
strncpy(name,pname);
noofpart++;
cout<<"create the no:"<<noofpart<<"of part"<<endl;
}
~Part()
{
noofpart--;
cout<<"destroy the no:"<<noofpart<<"of part"<<endl;
{
noofpart--;
cout<<"destroy the no:"<<noofpart<<"of part"<<endl;
}
static int number()
{
return no;
}
private:
static int noofpart=0;
int no;
char name[40];
};
int main()
{
Part p1;
Part p2;
return 1;
}
[解决办法]


#include <iostream>
#include <string>

using namespace std;

class Part
{
public:
Part(char *pname="no name")
{
strncpy(name,pname,40);//这里漏了个参数
noofpart++;
cout<<"create the no:"<<noofpart<<" of part"<<endl;
}
~Part()
{
noofpart--;
cout<<"destroy the no:"<<noofpart<<" of part"<<endl;
}
int number() const //这个函数不能是static
{
return no;
}
private:
static int noofpart;//static数据成员要在类外定义时初始化。
int no;
char name[40];
};

int Part::noofpart = 0;

int main()
{
Part p1;
Part p2;
return 1;
}

读书人网 >C++

热点推荐