Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.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++_Arrays_Class_Vector_Dynamic - Fatal编程技术网

C++ 创建自定义向量类错误

C++ 创建自定义向量类错误,c++,arrays,class,vector,dynamic,C++,Arrays,Class,Vector,Dynamic,所以我试图创建一个自定义类,它的功能类似于向量。它应该有一个默认构造函数,用于创建容量为2的空向量,一个容量为n的参数化构造函数,一个析构函数,一个返回向量大小的函数,一个向量的容量,一个删除数据并将向量重置为容量2的函数,一个push_back函数将n放在向量的末尾,一个函数返回存储在n的值。我已经写出了大部分代码,但是每当我尝试使用.capacity()函数来显示“向量”的容量时,就会在driver.cpp文件中收到错误。错误是 “函数调用”和Vector::capacity:函数中的参数太

所以我试图创建一个自定义类,它的功能类似于向量。它应该有一个默认构造函数,用于创建容量为2的空向量,一个容量为
n
的参数化构造函数,一个析构函数,一个返回向量大小的函数,一个向量的容量,一个删除数据并将向量重置为容量2的函数,一个
push_back
函数将
n
放在向量的末尾,一个函数返回存储在
n
的值。我已经写出了大部分代码,但是每当我尝试使用
.capacity()
函数来显示“向量”的容量时,就会在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;
}
//Vectors.h
#包括“stdafx.h”
#包括“driver.h”
#包括“targetver.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类向量
{
double*arr;//指向此myvec的第一个元素的指针
int cap;//arr可以容纳的元素数(即基础数组的大小)
int n;//此myvec的大小
int sz=1;
//将基础阵列的容量增加到sz。如果sz
//小于当前容量,则不执行任何操作。
//创建一个空向量
空隙增加容量(内部sz)
{
如果(sz=cap)增加_容量(2*cap);
arr[n]=x;
++n;
}
双容量(int i,双val)
{

如果(i<0 | | i>=n)cout=n)cout您定义的
capacity
方法采用了两个参数,您所采用的方式是这样的。它可能不应该这样做,因为在您尝试使用它时,它应该完全依赖于对象,只返回您想要的值。您可以在提供参数之间进行选择:
Vector::capacity(i,var)
,或将定义更改为接受0个参数

根据我对实际
Vector
class方法
capacity()的理解在C++中,您不应该接受任何参数。但是,从我理解的关于“代码>容量<代码>的定义中,您正在修改一个组件,其中<强> DO <强>需要同时提供浮点和int,这是您编写<代码> vector::容量()/<代码> > < /P>时所不做的。
代码的第一部分似乎很奇怪(前面的
public:
),因为它不是构造函数,但似乎正在初始化向量。此外,变量
sz
应该重命名为
cap
,因为它是容量,而不是大小,变量
n
应该重命名为
sz
,因为它是大小。。(我理解你为什么感到困惑…)

为了方便自己,我把
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;
}
其余的函数似乎还可以。请注意,我可能没有正确地进行内存管理,尤其是在构造函数方面。如果我这样做了,我肯定会出错。我希望你能找到答案

编辑:在查看了
后推方法之后,我现在明白了为什么要使用
增加容量方法。下面是与我的代码相关的编辑:

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;
}
void增加容量(int新容量)
{
//如果(sz上限)。
double[new_cap]arr;//分配一个新数组-请注意,我再次没有使用指针,您可能需要更改此设置。
对于(int i=0;i
您可以编辑您的问题。没有理由在注释中添加更多信息-只需将其添加到您的问题中即可。您的
capacity
函数使用int和double
double capacity(int i,double val)
,但你调用它却没有参数..就像你的错误消息所说的那样。离题:我希望一个名为capacity的方法返回容量,而不是元素的值。这很奇怪。它违反了最不令人惊讶的法则。在这条道路上存在着黑暗面。如果你不理解
容量的定义看起来,你不可能编写这个类的其余部分。这里有一个可能的代码源,你或其他人已经努力使它适应你的类的要求,但出于某种莫名其妙的原因,将
set
重命名为
capacity
。唯一可能发生的方法是绝对缺乏un理解这两个函数应该做什么。在这一点上,我只是迷路了。我盯着这件事看了好几个小时,我不知道该怎么办了。我已经试过了,代码将在2小时内到期。以下是代码的原始要求:1.默认构造函数创建一个空向量。它的大小将为零,它的capacity将为2。请记住,大小是指当前存储在向量中的元素数。2.创建容量为n的向量的参数化构造函数。其大小最初为零。@jonthie定义4和5是您必须注意的:向量的容量必须预定义,它就像一个打开的插槽ll的大小,但可能更大。例如,默认构造函数的容量必须为2,但大小为0,因此
arr
应该是大小为2的空数组。请注意,大小为2的数组不是大小为2的向量。
capacity()
将返回
arr.length()
,因此在本例中,2.大小仅与容量相同。如果向量已满,则大小始终小于容量