关于模版与class的有关问题,是VS 2013的有关问题吗

关于模版与class的问题,是VS 2013的问题吗?
本帖最后由 u011873969 于 2013-12-31 01:14:00 编辑
程序很简单,定义一个固定大小的数组,可以进行添加元素(因为初始时它是partially-filled)、清空、返回元素个数等操作,并且重载了<<操作符,但是在demonstration里进行到
cout <<
这一步的时候出现连接错误,搞了半天没明白是哪里出了问题,求大神指教!出错信息为
1>Demonstration.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl listsavitch::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class listsavitch::GenericList<int> const &)" (??6listsavitch@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$GenericList@H@0@@Z),该符号在函数 _main 中被引用

1>D:\ebook\C++\Problem Solving with C++\Chapter17, Templates\GenericList\Debug\GenericList.exe : fatal error LNK1120: 1 个无法解析的外部命令

下面是三个文件的代码:

genericlist.h

#ifndef GENERICLIST_H
#define GENERICLIST_H
#include <iostream>
using namespace std;

namespace listsavitch
{
template<class ItemType>
class GenericList
{
public:
GenericList(int max);

~GenericList();

int length() const;

void add(ItemType new_item);

bool full() const;

void erase();

friend ostream& operator <<(ostream& outs,
const GenericList<ItemType>& the_list);

private:
ItemType *item;  // pointer to the dynamic array that holds the list.
int max_length;  // max number of items allowed on the list.
int current_length;  // number of items currently on the list.
};
}// listsavitch

#endif  // GENERICLIST_H


genericlist.cpp

#ifndef GENERICLIST_CPP
#define GENERICLIST_CPP
#include <iostream>
#include <cstdlib>
#include "genericlist.h" 
using namespace std;

namespace listsavitch
{
//Uses cstdlib:
template<class ItemType>
GenericList<ItemType>::GenericList(int max) : max_length(max),
current_length(0)
{
item = new ItemType[max];
}

template<class ItemType>
GenericList<ItemType>::~GenericList()
{
delete []item;
}

template<class ItemType>
int GenericList<ItemType>::length() const
{
return (current_length);
}

// Uses iostream and cstdlib:
template<class ItemType>
void GenericList<ItemType>::add(ItemType new_item)
{
if (full())
{
cout << "Error: adding to a full list.\n";
exit(1);
}
else
{
item[current_length] = new_item;
current_length = current_length + 1;
}
}

template<class ItemType>
bool GenericList<ItemType>::full() const
{
return (current_length == max_length);
}

template<class ItemType>
void GenericList<ItemType>::erase()
{
current_length = 0;
}
        // Uses iostream:
        template<class ItemType>
        ostream& operator <<(ostream& outs, const GenericList<ItemType>& the_list)
        {
    for (int i = 0; i < the_list.current_length; i++)
    outs << the_list.item[i] << endl;

    return outs;
        }


}// listsavitch

#endif 


demonstration.cpp

#include <iostream>
#include "genericlist.h"
#include "genericlist.cpp"

using namespace std;
using namespace listsavitch;

int main()
{
GenericList<int> first_list(3);
first_list.add(1);
first_list.add(2);
cout << "first_list = \n"
<< first_list;

getchar();
}

文章评论

关于模版与class的有关问题,是VS 2013的有关问题吗
团队中“技术大拿”并非越多越好
关于模版与class的有关问题,是VS 2013的有关问题吗
那些争议最大的编程观点
关于模版与class的有关问题,是VS 2013的有关问题吗
做程序猿的老婆应该注意的一些事情
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员眼里IE浏览器是什么样的
关于模版与class的有关问题,是VS 2013的有关问题吗
不懂技术不要对懂技术的人说这很容易实现
关于模版与class的有关问题,是VS 2013的有关问题吗
写给自己也写给你 自己到底该何去何从
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员和编码员之间的区别
关于模版与class的有关问题,是VS 2013的有关问题吗
漫画:程序员的工作
关于模版与class的有关问题,是VS 2013的有关问题吗
聊聊HTTPS和SSL/TLS协议
关于模版与class的有关问题,是VS 2013的有关问题吗
5款最佳正则表达式编辑调试器
关于模版与class的有关问题,是VS 2013的有关问题吗
初级 vs 高级开发者 哪个性价比更高?
关于模版与class的有关问题,是VS 2013的有关问题吗
鲜为人知的编程真相
关于模版与class的有关问题,是VS 2013的有关问题吗
Java 与 .NET 的平台发展之争
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员必看的十大电影
关于模版与class的有关问题,是VS 2013的有关问题吗
Web开发人员为什么越来越懒了?
关于模版与class的有关问题,是VS 2013的有关问题吗
十大编程算法助程序员走上高手之路
关于模版与class的有关问题,是VS 2013的有关问题吗
旅行,写作,编程
关于模版与class的有关问题,是VS 2013的有关问题吗
老美怎么看待阿里赴美上市
关于模版与class的有关问题,是VS 2013的有关问题吗
60个开发者不容错过的免费资源库
关于模版与class的有关问题,是VS 2013的有关问题吗
总结2014中国互联网十大段子
关于模版与class的有关问题,是VS 2013的有关问题吗
为啥Android手机总会越用越慢?
关于模版与class的有关问题,是VS 2013的有关问题吗
看13位CEO、创始人和高管如何提高工作效率
关于模版与class的有关问题,是VS 2013的有关问题吗
10个调试和排错的小建议
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员最害怕的5件事 你中招了吗?
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员的一天:一寸光阴一寸金
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员都该阅读的书
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员的鄙视链
关于模版与class的有关问题,是VS 2013的有关问题吗
Java程序员必看电影
关于模版与class的有关问题,是VS 2013的有关问题吗
中美印日四国程序员比较
关于模版与class的有关问题,是VS 2013的有关问题吗
为什么程序员都是夜猫子
关于模版与class的有关问题,是VS 2013的有关问题吗
什么才是优秀的用户界面设计
关于模版与class的有关问题,是VS 2013的有关问题吗
每天工作4小时的程序员
关于模版与class的有关问题,是VS 2013的有关问题吗
如何成为一名黑客
关于模版与class的有关问题,是VS 2013的有关问题吗
我的丈夫是个程序员
关于模版与class的有关问题,是VS 2013的有关问题吗
要嫁就嫁程序猿—钱多话少死的早
关于模版与class的有关问题,是VS 2013的有关问题吗
程序猿的崛起——Growth Hacker
关于模版与class的有关问题,是VS 2013的有关问题吗
如何区分一个程序员是“老手“还是“新手“?
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员应该关注的一些事儿
关于模版与class的有关问题,是VS 2013的有关问题吗
10个帮程序员减压放松的网站
关于模版与class的有关问题,是VS 2013的有关问题吗
“肮脏的”IT工作排行榜
关于模版与class的有关问题,是VS 2013的有关问题吗
Web开发者需具备的8个好习惯
关于模版与class的有关问题,是VS 2013的有关问题吗
我跳槽是因为他们的显示器更大
关于模版与class的有关问题,是VS 2013的有关问题吗
当下全球最炙手可热的八位少年创业者
关于模版与class的有关问题,是VS 2013的有关问题吗
程序员的样子
关于模版与class的有关问题,是VS 2013的有关问题吗
一个程序员的时间管理