2016-07-14 331 views
0

沒有宣佈每當我嘗試編譯我的代碼我得到一個錯誤:功能範圍

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

我想不通爲什麼發生這種情況的任何幫助將是編譯不勝感激!由於

UBVector.h

#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 

UBVector.cpp

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

Driver.cpp

#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

此錯誤是從WR ong語法。 int * insert(int *,const int)是一個函數,它返回名爲'insert'的'int *',而不是返回'int'且名稱爲'* insert'的函數。

因此,您需要在類的名稱空間中包含'insert',而不是'* insert'。

至於代碼,

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

在其他功能方面,你使用正確的語法,所以,我認爲,這只是一個錯誤,不是嗎?