Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;动态数组索引和容量问题_C++_Arrays_C++11_Dynamic Arrays - Fatal编程技术网

C++ C++;动态数组索引和容量问题

C++ C++;动态数组索引和容量问题,c++,arrays,c++11,dynamic-arrays,C++,Arrays,C++11,Dynamic Arrays,我的家庭作业是让我构建一个动态数组和其他函数。我已经设法完成了其余的问题,但有一个错误我不明白 当前,如果我设置D[0]=11和D[1]=12,则无论数组有多大,数组中的所有值都将变为12,同时其容量将变为12 以下是我认为相关的代码,我将根据每个请求提供更多 模板 dynarr类{ 私人: 国际能力; 达尼勒姆*A; 公众: dynarr():容量(0),A(NULL){}; dynarr(intn):容量(N),A(新的dynElem[N]){} dynarr(const dynarr和其

我的家庭作业是让我构建一个动态数组和其他函数。我已经设法完成了其余的问题,但有一个错误我不明白

当前,如果我设置
D[0]=11
D[1]=12
,则无论数组有多大,数组中的所有值都将变为12,同时其容量将变为12

以下是我认为相关的代码,我将根据每个请求提供更多

模板
dynarr类{
私人:
国际能力;
达尼勒姆*A;
公众:
dynarr():容量(0),A(NULL){};
dynarr(intn):容量(N),A(新的dynElem[N]){}
dynarr(const dynarr和其他);
~dynarr();
dynarr和运算符=(常数dynarr和其他);
dynElem和运算符[](int ndx)抛出(无效索引);
int getCapacity();
无效准备金(int newcap);
};
样板
dynarr::dynarr(const dynarr和其他)
{
容量=其他容量;
A=新动力装置[容量];
对于(int i=0;i
test.cpp

int main()
{
  dynarr<int> D(15);
  std::cout << "The capacity of D is " << D.getCapacity() << std::endl;
  D[0] = 11;
  D[1] = 12;
  std::cout << "D[0] = " << D[0] << std::endl; //12
  std::cout << "D[1] = " << D[1] << std::endl; //12
  std::cout << "The capacity of D is " << D.getCapacity() << std::endl; //12
  return 0;
}
intmain()
{
dynarrd(15);

std::cout我很惊讶你的代码实际上是在没有警告的情况下编译的(我的编译器会对我大喊大叫)。你“忘记”从
操作符[]
返回值。下面是你应该做的

template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{
    if (ndx < 0) {
        throw InvalidIndex("Array Index is negative");
    }
    else if (ndx > capacity) {
        throw InvalidIndex("Array Index is too large");
    }
    return A[ndx];   // here we return the element.
}

听起来,你可能需要学习如何使用调试器来完成代码。使用一个好的调试器,你可以逐行地执行程序,并查看它偏离了你所期望的地方。如果你要做任何编程,这是一个必不可少的工具。进一步的阅读:在C++中创建动态数组的习惯用法是使用< c。ode>std::vector
具有所需类型。您的
运算符[]在哪里
函数实际上返回了任何东西?@DanielKamilKozar相信我,它没有;-)!没有工作保证这样的东西。不。你说它返回了对
dynElem
对象的引用。如果你不返回任何东西,你将有未定义的行为。在你的情况下,它可能应该返回
a[ndx]
template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{
    if (ndx < 0) {
        throw InvalidIndex("Array Index is negative");
    }
    else if (ndx > capacity) {
        throw InvalidIndex("Array Index is too large");
    }
}

template <class dynElem>
int dynarr<dynElem>::getCapacity()
{
  return capacity;
}
template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{
    if (ndx < 0) {
        throw InvalidIndex("Array Index is negative");
    }
    else if (ndx > capacity) {
        throw InvalidIndex("Array Index is too large");
    }
    return A[ndx];   // here we return the element.
}
template<typename T>
class dynarr {
    std::size_t _capacity =0;    // set default values for initialisation
    std::unique_ptr<T[]> ptr;    // don't worry about de-allocation
  public:
    dynarr() = default;          // no need to define a destructor either
    dynarr(std::size_t N)
      : _capacity(N), ptr(new T[N]) {}
    dynarr(const dynarr&);
    dynarr& operator=(const dynarr&);
    T& operator[](size_t i);                // no need for throw()
    T const& operator[](size_t i) const;    // version for const access
    size_t capacity() const noexcept
      { return _capacity; }
    void reserve(size_t new_capacity);
};