很简单的c++问题 出现链接问题 想不明白
学习c++ primer书中 关于vector 自己实现一下 发现 类里面具体实现如果放在类头文件下编译通过 不放在头文件下单起 x.cpp然后包含头文件就不行 出错 说什么找不到 具体看实例
这是vector.h
- C/C++ code
#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;}; vector.cpp
- C/C++ code
#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;}main.cpp
- C/C++ code
#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);}编译器vs2010
错误如下
1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Vector<int>::push_back_new(int const &)" (?push_back_new@?$Vector@H@@QAEXABH@Z),该符号在函数 _main 中被引用
1>E:\STUDY\C++\Code\vs2010\allocator_vector_20120325\Debug\allocator_vector_20120325.exe : fatal error LNK1120: 1 个无法解析的外部命令
如果把前两个文件合一 就能编译通过但是会出现诡异的问题 要求我中断或者继续不知道是什么错误
- C/C++ code
#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;}问题出来了 1为什么设计实现分离就出错呢 2 合到一起出的是什么错呢 3 我注视的那一段 干什么用的呢
下面付原书代码 求解答原书设计实现分离的话也是一样的问题(改完我包含头了)
- C/C++ code
#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;}
新手什么也不懂 欢迎大家帮帮忙 好郁闷
[解决办法]
c++ class template要求实现和声明必须在一个文件当中。就是一个规则而已。这也是类模板实现的通常做法。编译器就是这么支持的,否则编译不能通过。