首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2014-01-03 
关于模版与class的问题,是VS 2013的问题吗?本帖最后由 u011873969 于 2013-12-31 01:14:00 编辑程序很简单

关于模版与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();
}


[解决办法]
不是分离编译的原因,对于friend这个声明改成如下形式:
class GenericList定义中:

        template<class T>
friend ostream& operator <<(ostream& outs,
const GenericList<T>& the_list);

热点排行