【发布时间】:2017-07-25 01:12:53
【问题描述】:
所以我正在尝试创建一个功能类似于矢量的自定义类。它应该有一个默认构造函数,它创建一个容量为 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;
}
以下是我的课程的相关要求:
- 创建空向量的默认构造函数。它的大小为零,容量为二。请记住,大小是指当前存储在向量中的元素数量。
- 创建容量为 n 的向量的参数化构造函数。它的大小最初为零。
- 返回向量大小的函数 size()。大小定义为已存储在向量中的整数值的数量。大小将随着整数值的添加而改变。
- 返回向量容量的函数 capacity( )。容量定义为可以存储在向量中的整数值的数量。容量随着向量的增长而变化。
【问题讨论】:
-
您可以编辑您的问题。没有理由在评论中添加更多信息 - 只需将其放在您的问题中即可。
-
你的
capacity函数接受一个 int 和一个 doubledouble capacity(int i, double val),但你调用它时不带参数......就像你的错误消息所说的那样。 -
离题:我希望一个称为容量的方法返回容量,而不是元素的值。这很奇怪。它确实违反了最小意外法则。沿着这条路走下去就是黑暗面。
-
如果你不明白
capacity的定义应该是什么样子,你不可能写完这个类的其余部分。 -
这是您的代码cs.sfu.ca/CourseCentral/125/tjd/vector_example.html 的一个可能来源您或其他人已努力使其适应您的课程要求,但由于某些莫名其妙的原因将
set重命名为capacity。发生这种情况的唯一方法是完全不了解这两个函数应该做什么。
标签: c++ arrays class vector dynamic