创建自定义向量类错误



所以我正在尝试创建一个像向量一样的自定义类。它应该有一个默认构造函数,该构造函数创建一个容量为 2 的空向量, 一个容量为n的参数化构造函数 ,一个析构函数,一个返回向量大小的函数,一个向量的容量,一个删除数据并将向量重置为容量 2 的函数, 一个push_back函数,用于将n放在向量的末尾,以及一个用于返回存储在n的值的函数。我已经写出了大部分代码,但是每当我尝试使用.capacity()函数显示"矢量"的容量时,驱动程序.cpp文件都会收到错误。错误是

函数调用中的参数太少,Vector::capacity:函数 不接受 0 个参数

每次我尝试在驱动程序中使用capacity()时.cpp。请帮忙

//Vectors.h
#include "stdafx.h"
#include "driver.h"
#include "targetver.h"
#include <iomanip>
#include <array>
#include <stdio.h>
#include <tchar.h>
#include <string>
using namespace std;

class Vector
{
double* arr;  // pointer to the first element of this myvec
int cap; // number of elements arr can hold (i.e. size of underlying array)
int n;        // size of this myvec
int sz = 1;
// Increases the capacity of the underlying array to be sz. If sz
// is smaller than the current capacity then nothing is done.
// create an empty vector
void increase_capacity(int sz)
{
if (sz <= cap) return;
double* new_arr = new double[sz];   // allocate a new array
for (int i = 0; i < cap; ++i)
{ // copy old vector into new one
new_arr[i] = arr[i];
}
cap = sz;                      // set the new capacity
delete[] arr;                       // delete the old vector
arr = new_arr;
}
public:
Vector()
{
arr = new double[sz];
}
Vector(int n)
{
arr = new double[n];
}
int size() const 
{
return n;
}
void push_back(double x) 
{
if (n >= cap) increase_capacity(2 * cap);
arr[n] = x;
++n;
}
double capacity(int i, double val)
{
if (i < 0 || i >= n) cout << ("range error");
arr[i] = val;
return val;
}
double at(int n) const 
{
if (n < 0 || n >= n) cout << ("range error");
return arr[n];
}
void clear()
{
delete[] arr;
Vector();
}
~Vector()
{       // destructor
delete[] arr;
}
};

//driver.cpp
const int TEST_VALUE1 = 21;
const int TEST_VALUE2 = 31;
const int TEST_VALUE3 = 41;
const int MAX = 12;
int main( )
{
// Create a default vector 
Vector sam;
// push some data into sam
cout << "nPushing three values into sam";
sam.push_back(TEST_VALUE1);
sam.push_back(TEST_VALUE2);
sam.push_back(TEST_VALUE3);
cout << "nThe values in sam are: ";
// test for out of bounds condition here
// and test exception 
for (int i = 0; i < sam.size( ) + 1; i++)
{
try
{
cout << sam.at(i) << " ";
}
catch(int badIndex)
{
cout << "nOut of bounds at index " << badIndex << endl;
}
}
cout << "n--------------n";
// clear sam and display its size and capacity
sam.clear( );
cout << "nsam has been cleared.";
cout << "nSam's size is now " << sam.size( );
cout << "nSam's capacity is now " << sam.capacity() << endl;   
cout << "---------------n";
// Push 12 values into the vector - it should grow
cout << "nPush 12 values into sam.";
for (int i = 0; i < MAX; i++)
sam.push_back(i);
cout << "nSam's size is now " << sam.size( );
cout << "nSam's capcacity is now " << sam.capacity( ) << endl;
cout << "---------------n";
cout << "nTest to see if contents are correct...";
// display the values in the vector
for (int i = 0; i < sam.size( ); i++)
{
cout << sam.at(i) << " ";
}
cout << "n--------------n";
cout << "nnTest Complete...";
cout << endl;
system("PAUSE");
return 0;
}

以下是对我的课程的要求,其中是相关的:

  1. 创建空向量的默认构造函数。它的大小将为零,容量将为两个。请记住,大小是指当前存储在向量中的元素数。
  2. 创建容量为 n 的向量的参数化构造函数。其大小最初将为零。
  3. 一个函数 size((,它返回向量的大小。大小定义为已存储在矢量中的整数值的数量。大小将随着整数值的添加而变化。
  4. 一个函数 capacity( (,它返回向量的容量。容量定义为可存储在矢量中的整数值的数量。容量随着矢量的增长而变化。

您定义的capacity方法采用两个参数,就像您制作它的方式一样。它可能不应该,因为当你尝试使用它时,它应该完全依赖于对象,只返回你想要的值。您可以选择提供参数:Vector::capacity(i, var),或更改定义以接受 0 参数。

根据我对C++中capacity()的实际Vector类方法的理解,您不应该接受任何论点。但是,根据我对capacity定义的了解,您正在修改一个组件,您确实需要同时提供浮点数和整数,而您在编写Vector::Capacity()时没有这样做。

代码的第一部分看起来很奇怪(前面public:(,因为它不是构造函数,但您似乎正在初始化一个向量。此外,您的变量sz应重命名为cap,因为它是容量,而不是大小,并且您的变量nsz,因为它是大小。(我明白你为什么感到困惑...

为了方便自己,我将double*改为double。随意将其改回来。我也把sz写成size,因为两个字母使它看起来更漂亮。

class Vector
{
// initialise array myvec in constructors.
int cap; // number of elements arr can hold (i.e. size of underlying array)
int size;        // size of this myvec (same as n)

此时,在构造函数之外"创建空向量"是没有用的。因此,我将此处的位移到了两个构造函数中。

public:
Vector()
{
double[2] arr; //creates an array of size two, that is empty.
cap = 2;
size = 0;
}
Vector(int n)
{
double[n] arr; //creates an array of size n, also empty.
cap = n;
size = 0;
}
int size() const 
{
return size;
}

void push_back(double x) 
{
if (size >= cap) increase_capacity(2 * cap);
arr[size] = x;
++size;
}
double capacity()
{
return cap;
}

其余功能似乎还可以。请注意,我可能没有正确进行内存管理,尤其是使用构造函数。如果我这样做了,我一定会弄错。我希望你能弄清楚这一点。

编辑:在查看了pushback方法之后,我现在明白为什么您有increase_capacity方法。这是相对于我的代码的编辑:

void increase_capacity(int new_cap)
{
//if (sz <= cap) return; THIS IS NOT NEEDED, since the function is only called when size > cap.
double[new_cap] arr;   // allocate a new array - Note that once again I'm not using pointers, you may want to change this.
for (int i = 0; i < cap; ++i)
{ // copy old vector into new one
new_arr[i] = arr[i];
}
cap = new_cap;                      // set the new capacity
delete[] arr;                       // delete the old vector
arr = new_arr;
}

最新更新