hhl987805837 2023-12-14 07:43 采纳率: 66.7%
浏览 4
已结题

arr3没有输出 如何解决


 
#include<iostream>
#include<string>
#include<new>
#include <algorithm>
#include <vector>
 
using namespace std;
class myarray{
    public:
        myarray();
        myarray(const myarray&m);
        myarray(int len);
        // myarray add(const myarray& a,const myarray& b);
    ~myarray();
    void setdata(int index,double data);
    double getdata(int index);
     double getlen()const;
    int result[];
    double* e;
    
    myarray &operator =(const myarray&);
    int &operator[](int i)const;
    myarray &operator+(const myarray & ma);
    friend ostream & operator << (ostream & os,const myarray & ma);
    friend istream & operator >> (istream & is,const myarray & ma);
    private:
    int len;
    int *space;
};
myarray::myarray(){this->len = 0;this->space = NULL;
}
myarray::myarray(int len)
{
if (len <= 0) {
 this->len = 0; return; }
 else{
this->len = len;
this->space = new int[this->len];
    }
}
myarray::~myarray(){
    if((this->space)!= NULL){
        delete this->space;
        this->space=NULL;
        len = 0;
    }
}
void myarray::setdata(int index,double data){
    if(this->space != NULL){this->space[index]=data;
    }
};
double myarray::getdata(int index){
    return this->space[index];
};
double myarray::getlen()const {return this->len;};
 
myarray& myarray::operator =(const myarray & m){
 
if (this == &m) { return *this; }
if (this->space != NULL){
delete[] this->space;
this->space = NULL;
}
}
int & myarray::operator [](int i)const {return this->space[i];}
 
ostream & operator << (ostream & os,const myarray & ma){
    for (int i=0;i<ma.getlen();i++){
        os<<ma[i]<<"\t";
        
    }
    return os;
} 
 
istream & operator >>(istream & is,myarray & ma){
    cout <<"输入"<<ma.getlen()<<"个数"<<endl;
    for (int i=0;i<ma.getlen();i++){is>>ma[i];
    } 
    return is;
}
 
 
myarray &myarray::operator+(const myarray &ma)
{
   myarray *t = new myarray(ma.getlen());
   for (int i = 0; i < ma.getlen(); ++i) 
   {
      t->space[i] = space[i] + ma[i]; 
   }
   return *t; 
}
 
 
 
 
int main(){
    int buff;
    cin>>buff;
    myarray arr1(buff);
    myarray arr2(buff);
    cin>>arr1;
    cin>>arr2;
    cout<<"arr2:"<<arr2<<endl;
    myarray arr3 = arr1 + arr2;
    cout<<"arr1+arr2="<<arr3<<endl;
} 

```

  • 写回答

1条回答 默认 最新

  • xbdcbd 2023-12-14 11:43
    关注

    没有验证,仅供参考

    
    ```c++
    #include <iostream>
    using namespace std;
    
    class myarray {
    public:
        myarray();
        myarray(const myarray& m);
        myarray(int len);
        ~myarray();
        void setdata(int index, double data);
        double getdata(int index);
        double getlen() const;
        myarray& operator=(const myarray& m);
        int& operator[](int i) const;
        myarray& operator+(const myarray& ma);
        friend ostream& operator<<(ostream& os, const myarray& ma);
        friend istream& operator>>(istream& is, myarray& ma);
    
    private:
        int len;
        int* space;
    };
    
    myarray::myarray() {
        len = 0;
        space = nullptr;
    }
    
    myarray::myarray(int len) {
        if (len <= 0) {
            this->len = 0;
            this->space = nullptr;
        }
        else {
            this->len = len;
            this->space = new int[this->len];
        }
    }
    
    myarray::myarray(const myarray& m) {
        len = m.len;
        space = new int[len];
        for (int i = 0; i < len; i++) {
            space[i] = m.space[i];
        }
    }
    
    myarray::~myarray() {
        if (space != nullptr) {
            delete[] space;
            space = nullptr;
            len = 0;
        }
    }
    
    void myarray::setdata(int index, double data) {
        if (space != nullptr) {
            space[index] = data;
        }
    }
    
    double myarray::getdata(int index) {
        return space[index];
    }
    
    double myarray::getlen() const {
        return len;
    }
    
    myarray& myarray::operator=(const myarray& m) {
        if (this == &m) {
            return *this;
        }
        if (space != nullptr) {
            delete[] space;
            space = nullptr;
        }
        len = m.len;
        space = new int[len];
        for (int i = 0; i < len; i++) {
            space[i] = m.space[i];
        }
        return *this;
    }
    
    int& myarray::operator[](int i) const {
        return space[i];
    }
    
    ostream& operator<<(ostream& os, const myarray& ma) {
        for (int i = 0; i < ma.len; i++) {
            os << ma.space[i] << "\t";
        }
        return os;
    }
    
    istream& operator>>(istream& is, myarray& ma) {
        cout << "Enter " << ma.len << " numbers" << endl;
        for (int i = 0; i < ma.len; i++) {
            is >> ma.space[i];
        }
        return is;
    }
    
    myarray& myarray::operator+(const myarray& ma) {
        myarray* t = new myarray(ma.len);
        for (int i = 0; i < ma.len; ++i) {
            t->space[i] = space[i] + ma.space[i];
        }
        return *t;
    }
    
    int main() {
        int buff;
        cin >> buff;
        myarray arr1(buff);
        myarray arr2(buff);
        cin >> arr1;
        cin >> arr2;
        cout << "arr2: " << arr2 << endl;
        myarray arr3 = arr1 + arr2;
        cout << "arr1 + arr2 = " << arr3 << endl;
    }
    
    

    ```

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上
  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
  • ¥15 netcore使用PuppeteerSharp截图
  • ¥20 这张图页头,页脚具体代码该怎么写?
  • ¥15 关于#sql#的问题,请各位专家解答!
  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂