| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 454 人关注过本帖
标题:用模块定义的这个链表,类函数不在头文件中为什么不能运行。
只看楼主 加入收藏
gaojiannew
Rank: 1
等 级:新手上路
帖 子:6
专家分:6
注 册:2013-8-27
结帖率:0
收藏
 问题点数:0 回复次数:2 
用模块定义的这个链表,类函数不在头文件中为什么不能运行。
List.h
#ifndef _LIST_H
#define _LIST_H
template<class T>
class List
{
public:
    void Initial();
    void Show();
protected:
    class Node
    {
        friend class List;
    public:
        Node(T element=0,Node* next=0)
        {
            data=element;
            link=next;
        }
    protected:
        T data;
        Node* link;
    };
    Node* first;
};
#endif
List.cpp
#include"List.h"
#include<iostream>
using namespace std;
template<class T>
void List<T>::Initial()
{
    int m;
    Node* temp;
    cout<<"pls input the num of the list:";
    cin>>m;
    first=new Node(m,0);
    while(1)
    {
        cin>>m;
        if(m==0)
            break;
        first=new Node(m,first);
    }
}
template<class T>
void List<T>::Show()
{
    for(Node* temp=first;temp;temp=temp->link)
        cout<<temp->data<<" ";
    cout<<endl;
}
test.cpp
#include"List.h"
void main()
{
    List<int> A;
    A.Initial();
    A.Show();
   
}

最后测试的结果是:
1>------ 已启动生成: 项目: ConsoleApplication6, 配置: Debug Win32 ------
1>  test.cpp
1>  List.cpp
1>  正在生成代码...
1>test.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall List<int>::Initial(void)" (?Initial@?$List@H@@QAEXXZ),该符号在函数 _main 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall List<int>::Show(void)" (?Show@?$List@H@@QAEXXZ),该符号在函数 _main 中被引用
1>C:\Users\Administrator\Documents\Visual Studio 2012\Projects\ConsoleApplication6\Debug\ConsoleApplication6.exe : fatal error LNK1120: 2 个无法解析的外部命令
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


搜索更多相关主题的帖子: element include public friend 
2013-10-05 22:21
blueskiner
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:227
专家分:707
注 册:2008-9-22
收藏
得分:0 
类模板需要展开,模板的编译方式,请参照宏。
2013-10-06 11:44
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
哎...回去再看看书吧...模板的实现要写在h里面,不支持声明...

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-10-08 14:19
快速回复:用模块定义的这个链表,类函数不在头文件中为什么不能运行。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014729 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved