2016-07-14 2 views
0

에 선언되지 고맙다! 감사합니다기능은 내 코드를 컴파일 할 때마다 나는 오류가 발생 범위

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'를 반환하는 함수가 아니라 'insert'라는 이름으로 'int *'를 반환하는 함수입니다.

따라서 '* 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--; 
    } 
} 

, 당신은, 그것은 단지 실수,되지 못했습니다 구문을 사용하기 때문에?

관련 문제