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

关于#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 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能