gpt4 book ai didi

c++ - 用数组 C++ 重载运算符 +=

转载 作者:搜寻专家 更新时间:2023-10-31 01:28:57 25 4
gpt4 key购买 nike

我正在做一个 C++ 程序。这是我必须做的:我创建一个我想要的大小的数组。该数组自动填充为 0

使用 operator += i 必须在 i 选择的位置插入 1。示例:

array += 2; 
will insert 1 at the index 2 of my array.

但是我该怎么做呢?

我的 .h 文件

#ifndef BITARRAY_H
#define BITARRAY_H
#include <ostream>

class bitArray
{
public:
bitArray(int n);
virtual ~bitArray();

bitArray& operator+=(const bitArray&); //this operator
bitArray& operator-=(const bitArray&);
int& operator[] (int x) {
return sortie[x];
}
protected:
private:
int sortie[];
int n;
};

//ostream& operator<<(ostream&, const bitArray&);
#endif // BITARRAY_H

我在cpp文件中的方法:

bitArray& bitArray::operator+=(const bitArray& i)
{
this ->sortie[i] = 1;
return *this;
}

但它不起作用。我的做法是否正确?

我的错误是:

no match for 'operator[]' (operand types are 'int [0]' and 'const bitArray')|

提前致谢!

最佳答案

no match for operator[] (operand types are 'int [0]' and 'const bitArray')|

错误非常明显,operator[] 需要整数类型,而您传递的是 bitArray 类类型。简单的解决方法是将其更改为整数。

但是,这里:

private:
int sortie[];
int n;

强烈推荐使用std::vector,它给出了一个连续的动态数组,而sortie[]是静态分配。像这样:

See live here

#include <iostream>
#include <vector>
#include <cstddef>

class bitArray
{
private:
std::vector<int> sortie;
public:
explicit bitArray(int size): sortie(size) {}
bitArray& operator+=(const std::size_t i)
{
if (0 <= i && i < sortie.size()) // check for (0 <= index < size) of the array
{
this ->sortie[i] = 1;
return *this;
}
else
{
// do your logic! for instance, I have done something like follows:
std::cout << "out of bound" << std::endl;
if(sortie.size() == 0) sortie.resize(1,0); // if the size of array == 0
}
return *this;
}
int operator[] (const std::size_t index)
{
return (0 <= index && index < sortie.size()) ? sortie[index] : -1;
}
};
int main ()
{
bitArray obj(3);
obj += 0; std::cout << obj[0] << std::endl;
obj += -2; std::cout << obj[-2] << std::endl;
obj += 22; std::cout << obj[22] << std::endl;
return 0;
}

更新:使用 C++17 特性 std::optional ,用可选的返回类型修改了上面的解决方案,这应该更具可读性。

See output in wandbox

#include <iostream>
#include <vector>
#include <cstddef>
#include <optional>

class bitArray
{
private:
std::vector<int> sortie;
public:
explicit bitArray(int size): sortie(size) {}
// optional is used as the return type
std::optional<bitArray> operator+=(const std::size_t i)
{
if (i < sortie.size()) // check for (0 <= index < size) of the array
{
this -> sortie[i] = 1;
return std::optional<bitArray>{*this};
}
std::cout << "out of bound operation+= \t";
return std::nullopt; // std::nullopt to create any (empty) std::optional
}
std::optional<int> operator[] (const std::size_t index)
{
if(index < sortie.size()) return std::optional<int>{sortie[index]};
else
{
std::cout << "out of bound operator[]: ";
return std::nullopt;
}
}
};
int main ()
{
bitArray obj(3);
obj += 0; std::cout << obj[0].value_or(-1) << std::endl;
obj += -2; std::cout << obj[-2].value_or(-1) << std::endl;
bitArray obj1(0);
obj1 += 22; std::cout << obj1[22].value_or(-1) << std::endl;
return 0;
}

关于c++ - 用数组 C++ 重载运算符 +=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com