看如下报错的原因是什么?谢谢

dragonsea 2012-08-10 06:25:13
代码如下:
头文件:vector.h

#ifndef VECTOR_H
#define VECTOR_H

template <typename object>
class vector
{
public:
explicit vector(int ini_size = 0);
vector(const vector &rhs);
~vector();
const vector & operator= (const vector &rhs);
void size() const;
void resize(int new_size);
void reserve(int new_capacity);
object & operator[](int index);
const object & operator[](int index) const;
void push_back(const object & x);
enum{SPARE_CACPCITY = 16};

private:
int the_size;
int the_capacity;
object *objects;
};

#endif


vector.cpp文件

#include "vector.h"
vector::vector(int init_size):the_size(init_size),the_capacity(init_size + SPARE_CAPACITY){
the_size = init_size;
objects = new object[the_capacity];
}

vector::vector(vector & rhs):objects(NULL){
operator=(rhs);
}

~vector::vector(){
delete [] objects;
}

const vector::vector & operator=(const vector & rhs){
if(this != &rhs){
delete [] objects;
the_size = rhs.size();
the_capactity = rhs.the_capacity;

objects = object[the_capacity];
for (int i = 0; i < the_size; i++){
objects[i] = rhs[i];
}
return *this;
}
}

void size() const{
return the_size;
}

void resize(int new_size){
if(new_size > the_capacity){
the_size = new_size * 2 + 1;
reserve(the_size);
}
the_size = new_size;
}

void reserve(int new_capacity){
if(new_capacity < the_capacity){
return;
}
object * old_objects = objects;
objects = new object[new_capacity];
for(int i=0; i < old_objects.size(); i++){
objects[i] = old_objects[i];
}
the_capacity = new_capacity;
delete [] old_objects;
}

object & operator[](int index){
return objects[index];
}

void push_back(object & x){
if(the_size == the_capacity){
reserve(2 * the_capacity + 1);
}
objects[the_size++] = x;
}


main.cpp文件

#include "vector.h"
#include <iostream>
#include <string>

using namespace std;

int main(){
string str("abc");
vector<string> vc;
vc.push_back(str);
cout << vc[0] << endl;
return 0;
}


三个文件在同一个目录
使用下面的命令编译 g++ main.cpp -o main

报错信息如下:
/tmp/cc93OlsO.o: In function `main':
main.cpp:(.text+0x66): undefined reference to `vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::vector(int)'
main.cpp:(.text+0x79): undefined reference to `vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::push_back(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:(.text+0x8a): undefined reference to `vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::operator[](int)'
main.cpp:(.text+0xb5): undefined reference to `vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~vector()'
main.cpp:(.text+0xc8): undefined reference to `vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~vector()'
collect2: ld returned 1 exit status

请问
1、报错是什么意思
2.代码那里有问题导致报错?
...全文
98 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dragonsea 2012-08-11
  • 打赏
  • 举报
回复
谢谢诸位。基本功还不扎实。经验一点点积累。
taodm 2012-08-10
  • 打赏
  • 举报
回复
另外,楼主,没事别搞些尽和标准库里东西重名的东西。
kingxuke 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

模版类需要把实现也放到头文件里面. 而且你的实现也不对, 写成内联函数的方式吧.
[/Quote]

首先:模板不支持分离编译。你需要把 vector 的声明和定义放在一个文件里实现,详细原因看我之前回的一个帖子http://topic.csdn.net/u/20120801/12/ced57105-fd96-4038-a60d-f72222e97251.html?seed=1846712970&r=79290238
其次:你上面贴的代码的逻辑错误及书写错误太多了,下面给出修改后的可运行版本:
头文件:vector.h

#ifndef VECTOR_H
#define VECTOR_H

template <typename object>
class vector
{
public:
explicit vector(int init_size = 0);
vector(const vector &rhs);
~vector();
const vector & operator= (const vector &rhs);

void size() const;
void resize(int new_size);
void reserve(int new_capacity);

object & operator[](int index);
const object & operator[](int index) const;
void push_back(const object & x);

enum {SPARE_CACPCITY = 16};

private:
int the_size;
int the_capacity;
object *objects;
};

template <typename object>
vector<object>::vector(int init_size):the_size(init_size),
the_capacity(init_size + SPARE_CACPCITY){
the_size = init_size;
objects = new object[the_capacity];
}

template <typename object>
vector<object>::vector(const vector & rhs):objects(NULL){
operator=(rhs);
}

template <typename object>
vector<object>::~vector(){
delete [] objects;
}

template <typename object>
const vector<object> & vector<object>::operator=(const vector & rhs){
if(this != &rhs){
delete [] objects;
the_size = rhs.size();
the_capacity = rhs.the_capacity;

objects = object[the_capacity];
for (int i = 0; i < the_size; i++){
objects[i] = rhs[i];
}
return *this;
}
}

template <typename object>
void vector<object>::size() const{
return the_size;
}

template <typename object>
void vector<object>::resize(int new_size){
if(new_size > the_capacity){
the_size = new_size * 2 + 1;
reserve(the_size);
}
the_size = new_size;
}

template <typename object>
void vector<object>::reserve(int new_capacity){
if(new_capacity < the_capacity){
return;
}
object * old_objects = objects;
objects = new object[new_capacity];
for(int i=0; i < the_size; i++){
objects[i] = old_objects[i];
}
the_capacity = new_capacity;
delete [] old_objects;
}

template <typename object>
object & vector<object>::operator[](int index){
return objects[index];
}

template <typename object>
void vector<object>::push_back(const object & x){
if(the_size == the_capacity){
reserve(2 * the_capacity + 1);
}
objects[the_size++] = x;
}

#endif

主文件:main.cpp

#include "vector.h"
#include <iostream>
#include <string>

using namespace std;

int main(){
string str("abc");
vector<string> vc;
vc.push_back(str);
cout << vc[0] << endl;
return 0;
}
// 输出结果:abc

希望楼主好好对比更改前后的代码,原来的错误很容易得出。Good Luck!
www_adintr_com 2012-08-10
  • 打赏
  • 举报
回复
模版类需要把实现也放到头文件里面. 而且你的实现也不对, 写成内联函数的方式吧.

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧