如何解决输入和索引运算符重载问题? 我尝试用C++语言实现索引和输入运算符重载([]和> >)。程序应该要求用户输入一个整数,然后它应该打印这个整数的数字,但是我的程序不能正常工作。我不明白问题出在哪里。我怎样才能解决这个问题?提前感谢你的帮助

如何解决输入和索引运算符重载问题? 我尝试用C++语言实现索引和输入运算符重载([]和> >)。程序应该要求用户输入一个整数,然后它应该打印这个整数的数字,但是我的程序不能正常工作。我不明白问题出在哪里。我怎样才能解决这个问题?提前感谢你的帮助,c++,operator-overloading,C++,Operator Overloading,我的程序给出的输出: Default constructor of Integer class Enter a 4 digits number: 6748 0th digit of the number: 6748 1st digit of the number: -858993460 2nd digit of the number: -858993460 3rd digit of the number : -858993460 Destructor of Integer class Destr

我的程序给出的输出:

Default constructor of Integer class
Enter a 4 digits number: 6748
0th digit of the number: 6748
1st digit of the number: -858993460
2nd digit of the number: -858993460
3rd digit of the number : -858993460
Destructor of Integer class
Destructor of Integer class
Default constructor of Integer class
Enter a 4 digits number: 6748
0th digit of the number: 8
1st digit of the number: 4
2nd digit of the number: 7
3rd digit of the number : 6
Destructor of Integer class
Destructor of Integer class
#include <iostream>
#pragma once
#ifndef INTEGER_H
#define INTEGER_H
#define size 4
using namespace std;

class Integer {
    friend istream& operator>>(istream& inp, Integer& obj); // >> operator overloading
private:
    int r, i;
    int ptr[size];

public:
    //Integer(); //default constructor
    Integer();
    Integer(int*);
    ~Integer(); //destructor

    int& operator[](int index);  //[] operator overloading
};
#endif // !INTEGER_H

#include <iostream>
#include "Integer.h"
using namespace std;

int main() {
    Integer a;


    cout << "Enter a 4 digits number: ";
    cin >> a;
    Integer a1(a);
        cout << "0th digit of the number: " << a[3] << endl;
        cout << "1st digit of the number: " << a[2] << endl;
        cout << "2nd digit of the number: " << a[1] << endl;
        cout << "3rd digit of the number : " << a[0] << endl;



    return 0;

}
我想要的输出:

Default constructor of Integer class
Enter a 4 digits number: 6748
0th digit of the number: 6748
1st digit of the number: -858993460
2nd digit of the number: -858993460
3rd digit of the number : -858993460
Destructor of Integer class
Destructor of Integer class
Default constructor of Integer class
Enter a 4 digits number: 6748
0th digit of the number: 8
1st digit of the number: 4
2nd digit of the number: 7
3rd digit of the number : 6
Destructor of Integer class
Destructor of Integer class
#include <iostream>
#pragma once
#ifndef INTEGER_H
#define INTEGER_H
#define size 4
using namespace std;

class Integer {
    friend istream& operator>>(istream& inp, Integer& obj); // >> operator overloading
private:
    int r, i;
    int ptr[size];

public:
    //Integer(); //default constructor
    Integer();
    Integer(int*);
    ~Integer(); //destructor

    int& operator[](int index);  //[] operator overloading
};
#endif // !INTEGER_H

#include <iostream>
#include "Integer.h"
using namespace std;

int main() {
    Integer a;


    cout << "Enter a 4 digits number: ";
    cin >> a;
    Integer a1(a);
        cout << "0th digit of the number: " << a[3] << endl;
        cout << "1st digit of the number: " << a[2] << endl;
        cout << "2nd digit of the number: " << a[1] << endl;
        cout << "3rd digit of the number : " << a[0] << endl;



    return 0;

}
我的整数.h类:

Default constructor of Integer class
Enter a 4 digits number: 6748
0th digit of the number: 6748
1st digit of the number: -858993460
2nd digit of the number: -858993460
3rd digit of the number : -858993460
Destructor of Integer class
Destructor of Integer class
Default constructor of Integer class
Enter a 4 digits number: 6748
0th digit of the number: 8
1st digit of the number: 4
2nd digit of the number: 7
3rd digit of the number : 6
Destructor of Integer class
Destructor of Integer class
#include <iostream>
#pragma once
#ifndef INTEGER_H
#define INTEGER_H
#define size 4
using namespace std;

class Integer {
    friend istream& operator>>(istream& inp, Integer& obj); // >> operator overloading
private:
    int r, i;
    int ptr[size];

public:
    //Integer(); //default constructor
    Integer();
    Integer(int*);
    ~Integer(); //destructor

    int& operator[](int index);  //[] operator overloading
};
#endif // !INTEGER_H

#include <iostream>
#include "Integer.h"
using namespace std;

int main() {
    Integer a;


    cout << "Enter a 4 digits number: ";
    cin >> a;
    Integer a1(a);
        cout << "0th digit of the number: " << a[3] << endl;
        cout << "1st digit of the number: " << a[2] << endl;
        cout << "2nd digit of the number: " << a[1] << endl;
        cout << "3rd digit of the number : " << a[0] << endl;



    return 0;

}
#包括
#布拉格语一次
#ifndef整数
#定义整数
#定义大小4
使用名称空间std;
类整数{
friend istream&运算符>>(istream&inp,Integer&obj);//>>运算符重载
私人:
int r,i;
int ptr[大小];
公众:
//整数();//默认构造函数
整数();
整数(int*);
~Integer();//析构函数
int&运算符[](int索引);//[]运算符重载
};
#endif/!整数
整数.cpp

#include "Integer.h"
Integer::Integer() {
    cout << "Default constructor of Integer class" << endl;
}
Integer::~Integer() {
    cout << "Destructor of Integer class" << endl;
}

Integer::Integer(int* p) {
    //n = ptr;
    if(size!=0){
        for (int i = 0; i < size; i++) {
            //memcpy(p, ptr, 4 * sizeof(int));
            p = ptr;
            //ptr[i] = p[i];
            //ptr[i]=p[i];
    }

    }
}
istream& operator >> (istream& inp, Integer &obj) {
    inp >> obj.ptr[size-1];
    return inp;

}

int& Integer::operator[](int index) {
    if (index >= 0 && index < size) {
        return ptr[index];
    }
    /*if (index >= size && size<0) {
        cout << "Wrong size!" << endl;
        exit(0);
    }
    return ptr[index];*/
}


#包括“Integer.h”
整数::整数(){
cout obj.ptr[size-1];
返回inp;
}
整数和整数::运算符[](整数索引){
如果(索引>=0&&index/*如果(index>=size&&size代码中有几个错误。
在输入函数中:

istream& operator >> (istream& inp, Integer &obj) {
    inp >> obj.ptr[size-1];
    return inp;

}
  • 当您输入
    1234
    时,实际情况是您将其完全存储在
    obj.ptr[size-1]
    中。您应该将每个数字切片并存储在单独的索引中

  • 您总是将输入的数字存储在
    ptr[size-1]
    中,它始终是数组中的最后一个元素。如果要对数字进行切片,则应增加/减少修改数组时的索引

  • 如果我是你,我可能会这样做:

    istream& operator >> (istream& inp, Integer &obj) {
        int input;
        inp >> input;
        while(sizePtr >= 0) {
            obj.ptr[sizePtr--] = input % 10;
            input /= 10;
        }
        return inp;
    }
    

    其中,
    sizePtr
    是一个非常量变量,其大小为零索引(-1)您的
    ptr
    对象数组。

    如果输入值为0010,则while条件可能会导致问题。捕捉得好,我没有想到前导零OP被固定为4位,因此最好运行4次分解。零没有害处。更新,感谢您指出它。
    整数(int*p)
    constructor有一个主要缺陷,但我还不知道这是否是您的经验来源