创建自定义Vector类错误

时间:2017-07-25 01:12:54

标签: c++ arrays class vector dynamic

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

  

函数调用中的参数太少'和Vector::capacity:功能   不带0个参数

每次我尝试在driver.cpp中使用capacity()。请帮忙

//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 << "\n\nTest Complete...";

cout << endl;
system("PAUSE");
return 0;
}

以下是我班级的要求,其中有相关内容:

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

1 个答案:

答案 0 :(得分:-1)

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

根据我对C ++中实际的Vector类方法capacity()的理解,你不应该接受任何参数。但是,根据我对您对capacity的定义的理解,您正在修改一个组件,需要同时提供一个浮点数和一个int,这是你不做的当你写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)

此时,没有用'#34;创建一个空向量&#34;在构造函数之外。因此我把这里的位移到了两个构造函数中。

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;
}