yiyuantz 2023-03-06 17:57 采纳率: 100%
浏览 29
已结题

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

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


//头文件:
#include <iostream>
#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<<num_strings<<": \""<<str
         <<"\" object create\n";
}

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

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

StringBad::operator <<(std::ostream & os,const StringBad & st)
{
    os<<st.str;
    return os;
 } 
//
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/763174690876165.png "#left")
运行文件:
#include <iostream>
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: "<<headline1 <<endl;
        cout<<"headline2: "<<headline2 <<endl;
        cout<<"sports: "<<sports<<endl;
        callme1(headline1);
        cout<<"headline1: "<<headline1 <<endl;
        callme2(headline2);
        cout<<"headline2: "<<headline2 <<endl;
        cout<<"Initialize one object to another:\n";
        StringBad sailor=sports;
        cout<<"sailor: "<<sailor<<endl;
        cout<<"Assign one object to another:\n";
        StringBad knot;
        knot=headline1;
        cout<<"knot: "<<knot<<endl;
        cout<<"Exiting the bloak.\n";
    }
    cout<<"End of main()\n";
    return 0;
}

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

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

img

  • 写回答

1条回答 默认 最新

  • 「已注销」 2023-03-06 18:08
    关注

    “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。
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 3月14日
  • 已采纳回答 3月6日
  • 创建了问题 3月6日

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错