Operator []重载返回错误的数据

时间:2019-04-15 19:14:07

标签: c++ operator-overloading

我遵循了TutorialsPoint中的运算符重载教程。

这是我的课程:

struct Decimal {

private:
    int16_t  exponent;
    uint8_t  length;
    uint16_t mantissa[DecimalMaxSize]{};
...
public:
    uint16_t &operator[](int i);
...

和我的实现:

uint16_t &Decimal::operator[](int i) {
    if( i > DecimalMaxSize ) {
        std::cout << "Index out of bounds" << std::endl;
        // return first element.
        return mantissa[0];
    }
    return mantissa[i];
}

当我现在想这样使用时:

Decimal *d = new Decimal(...);
uint16_t di = d[i];

我收到以下错误:“没有从'Decimal'到'uint16_t'的可行转换”

我该如何解决?

1 个答案:

答案 0 :(得分:2)

<?php if(isset($_POST['submit-form'])) { $name = $_POST['name']; $email = $_POST['email']; if(($_POST['name'] != "") && ($_POST['email']!= "")) { echo "This is your name: ".$name."<br>"; echo "This is your email: ".$email; // header('refresh=3;url = ../leden/index.php'); } } else { echo "You have entered nothing or your username and/or password is incorrect, please try again."; // header('refresh=3;url = /test-form1.php'); } ?> 将是对d[i];对象的数组进行索引的操作,因为Decimal是一个指针。为了在对象itlsef上调用重载的d,您应该首先取消引用该指针,然后编写operator [](*d)[i]