关于#c++#的问题:在DVCC++5.11中编写好类的头文件,程序运行文件然后总之通不过编译,求解//头文件:#include <iostream>

在DVC C++5.11中编写好类的头文件,函数实现文件,程序运行文件然后总之通不过编译,求解


//头文件:
#include 
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
    private:
        char * str;
        int len;
        static int num_strings;
    public:
        StringBad(const char * s);
        StringBad();
        ~StringBad();
        friend std::ostream & operator <<(std::ostream & os,const StringBad & st);
    
};
#endif
//函数实现文件
#include "StringBad.h"
using std::cout;

int StringBad::num_strings=0;

StringBad::StringBad(const * s)
{
    len=std::strlen(s);
    str=new char[len+1];
    std::strcpy(str,s);
    num_strings ++;
    cout<": \""<"\" object create\n";
}

StringBad::StringBad()
{
    len=4;
    str=new char[4];
    std::strcpy(str,"c++");
    num_strings++;
    cout<": \""<"\"default object create\n";
}

StringBad::~StringBad()
{
    cout<<"\""<"\"object deleted, ";
    --num_strings;
    cout<" left\n";
    delete []str;    
}

StringBad::operator <<(std::ostream & os,const StringBad & st)
{
    os<return os;
 } 
//
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/763174690876165.png "#left")
运行文件:
#include 
using std::cout;
#include "StringBad.h"
void callme1(StringBad &);
void callme2(StringBad);

int main()
{
    using std::endl;
    {
        cout<<"StringBad an inner block.\n";
        StringBad headline1 ("Celery Stalks at Midnight");
        StringBad headline2 ("Lettuce Prey");
        StringBad sports("Spinach Leave Bow1 for Dollars");
        cout<<"headline1: "<"headline2: "<"sports: "<callme1(headline1);
        cout<<"headline1: "<callme2(headline2);
        cout<<"headline2: "<"Initialize one object to another:\n";
        StringBad sailor=sports;
        cout<<"sailor: "<"Assign one object to another:\n";
        StringBad knot;
        knot=headline1;
        cout<<"knot: "<"Exiting the bloak.\n";
    }
    cout<<"End of main()\n";
    return 0;
}

void callme1(StringBad & rsd)
{
    cout<<"String passed by reference:\n";
    cout<<"\""<"\"n";
}

void callme2(StringBad sb)
{
    cout<<"String passed by value:\n";
    cout<<"\""<"\"n";
}

img

“Devil组”引证GPT后的撰写:
有以下几个问题:

  • 在函数实现文件的 StringBad::StringBad(const * s) 函数中,形参缺少类型定义,应该为 const char * s。
  • 在函数实现文件的 StringBad::operator <<(std::ostream & os,const StringBad & st) 函数中,缺少函数返回类型,应该为 std::ostream &。
  • 在运行文件中调用 callme1 和 callme2 函数时,缺少空格,应该为 cout<<"""<<rsd<<""\n"; 和 cout<<"""<<sb<<""\n";。
  • 在头文件中缺少头文件保护的结束 #endif。