第一个模板类(templet<class elemType> class xxxxx)

第一个模板类(templet<class elemType> class xxxxx)小弟是刚刚加入c++大队的,对c++中的基本知识只是稍微懂一点。今天在看《c++primer》中的模板类时,就将其中的程序在vs2008中写下,新建了Array.h和Array.cpp两个文件。主函数:Main.cpp1#include”stdafx.h”2#include<iostream>3#include”Array.h”45u…

大家好,又见面了,我是你们的朋友全栈君。

   小弟是刚刚加入c++大队的,对c++中的基本知识只是稍微懂一点。今天在看《c++ primer》中的模板类时,就将其中的程序在vs 2008中写下,新建了Array.h和Array.cpp两个文件。

主函数:

第一个模板类(templet<class elemType> class xxxxx)
ExpandedBlockStart.gif
Main.cpp

 1 
#include 

stdafx.h



 2 
#include 
<
iostream
>


 3 
#include 

Array.h



 4 


 5 

using
 
namespace
 std;

 6 

int
 main(
int
 argc, 
char
 
*
argv[])

 7 
{


 8 
    Array
<
char
>
 array;

 9 
    cout
<<
array.size()
<<
endl;

10 


11 
    
char
 z;

12 
    cin 
>>
z;

13 
    
return
 
0
;

14 
}

15 


16 

 

 头文件:

第一个模板类(templet<class elemType> class xxxxx)
ExpandedBlockStart.gif
Array.h

 1 
#pragma
 once


 2 


 3 


 4 
template
<
class
 elemType
>


 5 

class
 Array

 6 
{


 7 

public
:

 8 
    

 9 
    
explicit
 Array(
int
 sz
=
DefaultArraySize);

10 
    Array(Array
<
elemType
>
 
*
array ,
int
 array_size);

11 
    Array(
const
 Array 
&
rhs);

12 


13 
    
~
Array(
void
);

14 


15 
    
//
判断是否相等操作


16 

    
bool
 
operator
==
(
const
 Array
&
)
const
;

17 
    
bool
 
operator
!=
(
const
 Array
&

const
;

18 


19 
    
//
赋值操作符


20 

    Array
&
 
operator
 
=
(
const
 Array
&
);

21 
    
int
 size() 
const
{

return
 _size;};

22 


23 
    elemType
&
 
operator
[](
int
 index);

24 
    
void
 sort();

25 
    elemType min();

26 
    elemType max() ;

27 
    
//
************************************

28 
    
//
 Method:    find

29 
    
//
 FullName:  IntArray::find

30 
    
//
 Access:    public 

31 
    
//
 Returns:   int

32 
    
//
 Qualifier:查找数组中是否存在特定的值。

33 
    
//
 Parameter: int value

34 
    
//
************************************


35 

    
int
 find(
const
 elemType 
&
value);

36 

protected
:

37 
    
void
 Init(
int
 sz,elemType 
*
array);

38 


39 

private
:

40 
    
static
 
const
 
int
 DefaultArraySize
=
12
;

41 
    
int
 _size;

42 
    elemType 
*
ia;

43 
    
bool
 sorted;

44 
};

45 

 

Array头文件的实现:

第一个模板类(templet<class elemType> class xxxxx)
ExpandedBlockStart.gif
Array.cpp

  1 
#include 

StdAfx.h



  2 
#include 

Array.h



  3 
#include 
<
cassert
>


  4 


  5 
template
<
class
 elemType
>
 Array
<
elemType
>
::Array(
int
 sz)

  6 
{


  7 
     Array
<
elemType
>
::Init(sz,
0
);

  8 
}

  9 


 10 


 11 
template
<
class
 elemType
>
  Array
<
elemType
>
::Array(Array
<
elemType
>
 
*
array ,
int
 array_size)

 12 
{


 13 
    Array
<
elemType
>
::Init(array_size,array);

 14 
}

 15 


 16 


 17 
template
<
class
 elemType
>
 Array
<
elemType
>
::Array(
const
 Array
<
elemType
>
 
&
rhs)

 18 
{


 19 
    Array
<
elemType
>
::Init(rhs.size(),rhs.ia);

 20 
}

 21 


 22 
template
<
class
 elemType
>
 Array
<
elemType
>
::
~
Array(
void
)

 23 
{


 24 
    delete []ia;

 25 
    std::cout
<<

析构函数作用中。。。。。

<<
std::endl;

 26 
}

 27 
template
<
class
 elemType
>


 28 

void
 Array
<
elemType
>
::Init(
int
 sz,elemType 
*
array)

 29 
{


 30 
    _size 
=
sz;

 31 
    ia
=
new
 elemType[_size];

 32 
    sorted
=
false
;

 33 


 34 
    
for
 (
int
 ix
=
0
;ix
<
_size;
++
ix)

 35 
    {


 36 
        
if
 (
!
array)

 37 
            ia[ix]
=
0
;

 38 
        
else


 39 
            ia[ix]
=
array[ix];

 40 
    }

 41 
}

 42 
template
<
class
 elemType
>


 43 
elemType
&
 Array
<
elemType
>
::
operator
 [](
int
 index)

 44 
{


 45 
    assert(index
>
0
||
index
<
size());

 46 
        
return
 ia[index];

 47 
}

 48 


 49 


 50 

//
************************************

 51 

//
 Method:    find

 52 

//
 FullName:  IntArray::find

 53 

//
 Access:    public 

 54 

//
 Returns:   int

 55 

//
 Qualifier:查找数组中是否存在特定的值。

 56 

//
 Parameter: int value

 57 

//
************************************


 58 

template
<
class
 elemType
>


 59 

int
 Array
<
elemType
>
::find(
const
 elemType 
&
value)

 60 
{


 61 
    
for
 (
int
 ix
=
0
;ix
<
_size;
++
ix)

 62 
    {


 63 
        
if
(ia[ix]
==
value)

 64 
            
return
 ix
+
1
;

 65 
    }

 66 
}

 67 
template
<
class
 elemType
>


 68 
elemType Array
<
elemType
>
::max()

 69 
{


 70 
    
return
 ia[
0
];

 71 
}

 72 


 73 
template
<
class
 elemType
>


 74 
elemType Array
<
elemType
>
::min()

 75 
{


 76 
    
return
 ia[size()

1
];

 77 
}

 78 


 79 
template
<
class
 elemType
>


 80 

void
 Array
<
elemType
>
::sort()

 81 
{


 82 
    elemType temp;

 83 
    
//
如果已经排序,返回。


 84 

    
if
(sorted)

 85 
        
return
;

 86 


 87 
    
for
 (
int
 i
=
0
;i
<
size()

1
;
++
i)

 88 
    {


 89 
        
for
(
int
 j
=
1
;j
<
size()

i;
++
j)

 90 
        {


 91 
            
if
(ia[i]
<
ia[j])

 92 
            {


 93 
                temp
=
ia[j];

 94 
                ia[j]
=
ia[i];

 95 
                ia[i]
=
temp;

 96 
            }

 97 
        }

 98 
    }

 99 


100 


101 
    
//
设置为以排序。


102 

    sorted 
=
true
;

103 
}

104 


105 

 

但是在编译的时候vs 2008提示“错误 1 error LNK2019: 无法解析的外部符号 “public: __thiscall Array<char>::~Array<char>(void)” (??1?$Array@D@@QAE@XZ),该符号在函数 _main 中被引用 Esonion.obj Esonion” 。

 

    到网上找了好久,才找到原来是vs 2008不支持模板定义和实现分离。将Array.cpp的内容拷贝到Array.h中,在编译就可通过了。我在c-free中试了,也是不可以。看来这两个编译器都不支持模板定义和实现分离。

    特意记下来,c++学习路上的一个脚印。

 

转载于:https://www.cnblogs.com/mht_two/archive/2010/10/10/1847338.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/144585.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • spring使用aspectj_你可知晓

    spring使用aspectj_你可知晓【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)http://blog.csdn.net/javazejian/article/details/54629058出自【zejian的博客】关联文章:关于SpringIOC(DI-依赖注入)你需要知道的一切关于SpringAOP(AspectJ)你该知晓的一切本篇是年后第一篇博文,由于博主用了不少

    2022年8月11日
    7
  • JAVA数组去重方法

    JAVA数组去重方法记录JAVA中数组去重方法1利用Set的特性去重复 Setset=newHasSet(); for(intx=0;x<arr.length;x++){ set.add(arr[x]); }最后set里面的值就是arr数组去重后的所有元素,但是set中数据是无序的,会打乱原本的顺序。2如果想保留原本的顺序就使用有顺序,不重复特点的链表的哈希集合 LinkedHasSet<Object>temp=newLinkedHasSet<>()

    2022年6月29日
    26
  • laravel where orwhere的写法

    laravel where orwhere的写法

    2021年11月10日
    43
  • xsync集群分发脚本的改良[通俗易懂]

    xsync集群分发脚本的改良[通俗易懂]集群分发脚本xsync带多参数1.0增强了一下带参个数xsync1.0#!/bin/bash#校验参数pcount=$#if(($pcount==0))then exitfi#获取用户名user=`whoami`#获取文件名,路径for((i=1;i<=$#;i++))#对多个传参进行分析dob=${!i} #这里用到了“间接变量”语法fname=`basename$b`dname=`dirname$b`dir=`cd$dname;pwd`

    2022年6月1日
    31
  • MyBatisPlus IService详解

    IService的使用方法需要配置MyBatisPlus才能使用publicinterfaceUserService2extendsIService<User>{}@ServicepublicclassUserServiceImplextendsServiceImpl<UserMapper,User>implementsUserService2{}@RestControllerpublicclassUserController.

    2022年4月7日
    198
  • 软件著作权的源代码_软件著作权源码被泄露

    软件著作权的源代码_软件著作权源码被泄露packagecom.example.shuoya;importjava.util.Set;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.database.Cursor;importandroid.data…

    2022年9月22日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号