很简单的c++问题 出现链接问题 想不明白
学习c++ primer书中 关于vector 自己实现一下 发现 类里面具体实现如果放在类头文件下编译通过 不放在头文件下单起 x.cpp然后包含头文件就不行 出错 说什么找不到 具体看实例
这是vector.h
#include<memory>#include<iostream>//using namespace std;using std::cout; using std::endl;template<class Type>class Vector{public: Vector():elments(0),first_free(0),end(0){}; void push_back_new(const Type&);private: void realloc(); static std::allocator<Type> alloc; Type *elments; Type *first_free; Type *end;}; #include"vector.h"#include<algorithm>using std::allocator;template <class T> allocator<T> Vector<T>::alloc;using std::max;using std::uninitialized_copy;template<class Type> void Vector<Type>::realloc(){ std::ptrdiff_t size = first_free - elments; std::ptrdiff_t newcapacity = 2* max(size,0); Type *newelments = alloc.allocate(newcapacity); uninitialized_copy(elments,first_free,newelments); for(Type *p = first_free;p != elments;) alloc.destroy(--p); if(elments) alloc.deallocate(elments,end-elments); elments = newelments; first_free = elments + size; end = elments + newcapacity;}template<class Type> void Vector<Type>::push_back_new(const Type& t){ if(first_free == end) realloc(); alloc.construct(first_free,t); ++first_free;}#include"vector.h"int main (){ Vector<int>vec; vec.push_back_new(5); vec.push_back_new(5); vec.push_back_new(5); vec.push_back_new(5); vec.push_back_new(5);}#include<memory>#include<iostream>//using namespace std;using std::cout; using std::endl;template<class Type>class Vector{public: Vector():elments(0),first_free(0),end(0){}; void push_back_new(const Type&);private: void realloc(); static std::allocator<Type> alloc; Type *elments; Type *first_free; Type *end;}; #include<algorithm>using std::allocator;template <class T> allocator<T> Vector<T>::alloc;/*这句非常不明白 按照原书代码不加这句不行我不明白的是 alloc明明是我类里定义的 为什么还得加这句那这句什么意思呢 作用呢*/using std::max;using std::uninitialized_copy;template<class Type> void Vector<Type>::realloc(){ std::ptrdiff_t size = first_free - elments; std::ptrdiff_t newcapacity = 2* max(size,0); Type *newelments = alloc.allocate(newcapacity); uninitialized_copy(elments,first_free,newelments); for(Type *p = first_free;p != elments;) alloc.destroy(--p); if(elments) alloc.deallocate(elments,end-elments); elments = newelments; first_free = elments + size; end = elments + newcapacity;}template<class Type> void Vector<Type>::push_back_new(const Type& t){ if(first_free == end) realloc(); alloc.construct(first_free,t); ++first_free;}#include <iostream>using std::cout; using std::endl;#include <memory>// psuedo-implementation of memory allocation strategy for a vector-like classtemplate <class T> class Vector {public: Vector(): elements(0), first_free(0), end(0) { } void push_back(const T&); size_t size() const { return first_free - elements; } size_t capacity() const { return end - elements; } // . . . T& operator[](size_t n) { return elements[n]; } const T& operator[](size_t n) const { return elements[n]; }private: static std::allocator<T> alloc; // member to handle allocation void reallocate(); // get more space and copy existing elements T* elements; // pointer to first element in the array T* first_free; // pointer to first free element in the array T* end; // pointer to one past the end of the array // . . .};#include <algorithm>using std::allocator;template <class T> allocator<T> Vector<T>::alloc;using std::max;using std::uninitialized_copy;template <class T> void Vector<T>::reallocate(){ // compute size of current array and allocate space for twice as many elements std::ptrdiff_t size = first_free - elements; std::ptrdiff_t newcapacity = 2 * max(size, 1); // allocate space to hold newcapacity number of elements of type T T* newelements = alloc.allocate(newcapacity); // construct copies of the existing elements in the new space uninitialized_copy(elements, first_free, newelements); // destroy the old elements in reverse order for (T *p = first_free; p != elements; /*empty*/ ) alloc.destroy(--p); // deallocate cannot be called on a 0 pointer if (elements) // return the memory that held the elements alloc.deallocate(elements, end - elements); // make our data structure point to the new elements elements = newelements; first_free = elements + size; end = elements + newcapacity;}template <class T> void Vector<T>::push_back(const T& t){ // any space left? if (first_free == end) reallocate(); // gets more space and copies existing elements to it // construct a copy t in the element to which first_free points alloc.construct(first_free, t); ++first_free;}int main(){ Vector<int> vi; for (int i = 0; i != 10; ++i) { vi.push_back(i); cout << vi[i] << endl; } for (int i = 0; i != 10; ++i) cout << vi[i] << endl; return 0;}