新生的问题,弄了好不久,不能编译通过,大家看一看,在线等!!
//类定义 stringbad.h#include<iostream>
using namespace std;
#ifndef stringbad_H_
#define stringbad_H_
class stringbad
{
private:
char *str;
int len;
static int num_strings;
public:
stringbad();
stringbad(const char *s);
~stringbad();
//friend function
friend ostream &operator<<(ostream &os,const ostream &st);
};
#endif
//类方法 stingbad.cpp
#include<iostream>
#include<cstring>
#include"stringbad"
using namespace std;
int stringbad::num_strings=0;//初始华静态数
stringbad::stringbad()
{
len=4;
str=new char[4];
strcpy(str,"C++");
num_strings++;
cout<<num_stirngs<<":\"<<str<<"defaultt object created";
}
stringbad::stringbad(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
cout<<num_strings<<":\"<<str<<"object created"<<endl;
}
stringbad::~stringbad()
{
cout<<"\"<<str<<"\"object delete"<<endl;
--num_strings;
cout<<num_strings<<"left\n";
delete [] str;
}
ostream &operator<<(ostream &os,const stringbad & st)
{
os<<st.str;
return os;
}
//主函数
#include<iostream>
#include"stringbad.h"
using namespace std;
//void callme1(stringbad &);
//void callme2(stringbad);
int main()
{
stringbad headline1("Celery Stalks at Midnight");//创建一个headline1对象
stringbad headline2("lettuce Prey");
stringbad sports("Spinach Leaves Bow1 for Dollars");
cout<<"headline1"<<headline1<<endl;
cout<<"headline2"<<headline2<<endl;
cout<<"sprots"<<sports;
return 0;
}