每当我尝试编译代码时,都会收到错误:
UBVector.cpp: In function ‘int UBVector::* insert(int*, int)’: UBVector.cpp:95:33: error: ‘reserve’ was not declared in this scope reserve(2*current_capacity);
我正在使用g++ driver.cpp UBVector.cpp
我无法弄清楚为什么会这样,任何帮助都会受到赞赏!感谢
#ifndef _UBVECTOR_H
#define _UBVECTOR_H
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
class UBVector{
public:
void swap(UBVector& x);
void reserve(size_t n);
//constructors and assignment operator
UBVector(const UBVector &x); //copy constuctor
explicit UBVector(size_t n=0, int val=0);
UBVector(int *pBegin, int *pEnd);
UBVector& operator = (const UBVector &x);
//deconstructor
~UBVector();
//capacity
size_t size() const;
size_t capacity() const;
//
int *begin();
int *end();
//modifiers
int *insert(int *pPosition, const int val);
int *erase(int *pPosition);
void push_back(const int &val);
void pop_back();
//accessors
int& at(size_t n);
int& operator[](size_t n);
int& front();
int& back();
//
private:
size_t num_items; //number of items currently in array
size_t current_capacity; //current capacity of array
static const size_t INITIAL_CAPACITY; //initial capacity of array
int *item_ptr; //points to start of array
};
#endif
#include "UBVector.h"
#include <iostream>
#include <string>
using namespace std;
const size_t UBVector::INITIAL_CAPACITY = 5; //allocate array space and size
size_t num_items = 0;
size_t current_capacity = 0;
int *item_ptr;
//constructor that creates an array of size n and sets each element to a variable val
UBVector::UBVector(size_t n, int val){
num_items = n;
current_capacity = max(n, INITIAL_CAPACITY);
item_ptr = new int[current_capacity];
for(int i=0; i<current_capacity; i++){
item_ptr[i] = val;
}
}
UBVector::UBVector(const UBVector& x)
: num_items(x.num_items), current_capacity(x.num_items), item_ptr(new int[x.num_items])
{
for(size_t i=0; i<num_items; i++){
item_ptr[i] = x.item_ptr[i];
}
}
UBVector::UBVector(int *pBegin, int *pEnd){
num_items = *pEnd - *pBegin;
current_capacity = num_items;
item_ptr = new int[current_capacity];
for(int i=*pBegin; i<=*pEnd; i++){
item_ptr[i] = i;
}
}
UBVector::~UBVector(){
delete [] item_ptr;
}
void UBVector::swap(UBVector& x){
std::swap(num_items, x.num_items);
std::swap(current_capacity, x.current_capacity);
std::swap(item_ptr, x.item_ptr);
}
UBVector& UBVector::operator= (const UBVector& x){
UBVector temp(x);
swap(temp);
return *this;
}
//returns current size of the vector(number of elements)
size_t UBVector::size() const{
return num_items;
}
//returns the current capacity of the vector
size_t UBVector::capacity() const{
return current_capacity;
}
//returns pointer to first element in vector
int* UBVector::begin(){
int x = item_ptr[0];
int *pX = &x;
return pX;
}
//returns pointer to last element in vector
int* UBVector::end(){
int x = item_ptr[num_items];
int *pX = &x;
return pX;
}
//checks if array is full, if it is then it creates a new array with 2x the previous capacity(or n, whichever is larger) and copies all the data from the previous array
void UBVector::reserve(size_t n){
if(n > current_capacity){
current_capacity = max(n, 2*current_capacity);
int *new_data_ptr = new int[current_capacity];
for(size_t i=0; i<num_items; i++){
new_data_ptr[i] = item_ptr[i];
}
delete [] item_ptr;
item_ptr = new_data_ptr;
}
}
//inserts element into vector before the element at pPosition, if array is full array size is the increased
int UBVector::*insert(int *pPosition, const int val){
if(num_items == current_capacity){
reserve(2*current_capacity);
}
for(size_t i=num_items; i>*pPosition; --i){
item_ptr[i] = item_ptr[i-1];
}
item_ptr[*pPosition] = val;
++num_items;
}
//removes element at pPosition from vector, shifts all elements over and empties last element in vector
int UBVector::*erase(int *pPosition){
if(*pPosition < num_items){
for(size_t i=*pPosition; i<num_items-1; ++i){
item_ptr[i] = item_ptr[i+1];
}
item_ptr[num_items-1] = int();
num_items--;
}
}
//adds element to the end of the vector if array is not full, if it is then a larger array will be created using the reserve method
void UBVector::push_back(const int& val){
if(num_items == current_capacity){
reserve(2*current_capacity);
}
item_ptr[num_items++] = val;
}
//erases last element in vector
void UBVector::pop_back(){
int x = num_items-1;
int *pX = &x;
erase(pX);
}
//returns reference to element at n in vector
int& UBVector::at(size_t n){
return item_ptr[n];
}
//returns reference to element at n, if n is larger than than the number of items in the vector, an out of range exception is thrown
int& UBVector::operator[](size_t n){
if(n < num_items){
return item_ptr[n];
}
else{
throw out_of_range("index is out of range");
}
}
//returns reference to first element in vector
int& UBVector::front(){
return (*this)[0];
}
//returns referece to last element in vector
int& UBVector::back(){
return (*this)[num_items-1];
}
#include <iostream>
#include <cstdlib>
#include "UBVector.h"
using namespace std;
int main(){
UBVector ubv(3);
ubv[0] = 5; ubv[1] = 6; ubv[2] = 7;
cout << ubv[0] << ubv[1] << ubv[2] << endl;
ubv.front() = 85;
ubv.back() = 95;
cout << ubv.front() << ubv.back() << endl;
}
答案 0 :(得分:0)
此错误来自您错误的语法。
int * insert(int *,const int)是一个返回&#39; int *&#39;的函数。名称&#39;插入&#39;,而不是返回&#39; int&#39;名字&#39; *插入&#39;。
因此,您需要包含&#39; insert&#39;到您班级的名称空间,而不是&#39; *插入&#39;。
作为代码,
//inserts element into vector before the element at pPosition, if array is full array size is the increased
int *UBVector::insert(int *pPosition, const int val){
if(num_items == current_capacity){
reserve(2*current_capacity);
}
for(size_t i=num_items; i>*pPosition; --i){
item_ptr[i] = item_ptr[i-1];
}
item_ptr[*pPosition] = val;
++num_items;
}
//removes element at pPosition from vector, shifts all elements over and empties last element in vector
int *UBVector::erase(int *pPosition){
if(*pPosition < num_items){
for(size_t i=*pPosition; i<num_items-1; ++i){
item_ptr[i] = item_ptr[i+1];
}
item_ptr[num_items-1] = int();
num_items--;
}
}
在其他功能中,你使用正确的语法,所以,我认为,这只是一个错误,不是吗?