| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 422 人关注过本帖
标题:请教一下关于模板类的问题
只看楼主 加入收藏
coolman366
Rank: 1
等 级:新手上路
帖 子:7
专家分:5
注 册:2009-11-14
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
请教一下关于模板类的问题
语法没出错,就是运行得不到结果,想了半天没找出错误,敢情哪位大虾指点下,不胜感激,本人在线:
代码如下:
//orderlink.h
#ifndef ORDERLIHK_H
#define ORDERLINK_H

#include <iostream>
using namespace std;
template <class T>

class orderlist
{
    public:
        orderlist();
        bool isEmpty();  //judge if the list is empty
        const int getlength();
        const int getvalue (const int);
        void addnum(const T&);
        void insert(const int ,const T ); //insert a element
        void del(const int ); //delete a element
        void search(const T );//searth a value
        void change(const int ,const T );//change the value
    private:
 //the value of element
         int length;  //the length of list
        //the position of list
        T* orlist;
};

template <class T>
orderlist<T>::orderlist()
{
    T* orlist= new T [100];
    length=0;
}

template <class T>
void orderlist<T>::addnum(const T &val)
{
   
    orlist[length]=val;
    length++;
}
template <class T>
bool orderlist<T>::isEmpty()
{
    return length==0;
}

template <class T>
const int orderlist<T>::getlength()
{
    return length;
}

template <class T>
const int orderlist<T>::getvalue (const int post)
{

    return orlist[post];
}
template <class T>
void orderlist<T>::insert(const int post,const T val)//insert a element
{

 if(post<=length){
  for (int i=length;i>post;i--)
        orlist[i]=orlist[i-1];
 orlist[post]=val;
 length++;
 cout<<"insert the value "<<val<<" in position "<<post<<" succefully"<<endl;
 }
 else
     cout<<post<<"is too long"<<endl;
}
template <class T>
void orderlist<T>::del(const int post) //delete a element
{
 if(post<=length){
 for(int i=post;i<length-1;i++)
     orlist[i]=orlist[i+1];
 cout<<"delete the element of position"<<p<<"succefuully"<<endl;
 }
 length++;
 else
     cout<<post<<"is to long"<<endl;
}
template <class T>
void orderlist<T>::search(const T val)//search a value
{
    for(int i=0;i<length;i++){
        if(val==orlist[i])
            cout<<"value"<<val<<"is found in position "<<i<<endl;
       else
           cout<<"value"<<val<<"is not found in position"<<endl;
    }
}

template <class T>
void orderlist<T>::change(const int post,const T val)//change the value

{
    if(post<length){
  orlist[post]=value;
  cout<<"value "<<value<<"is sucessful changed at position"<<post<<endl;
    }
    else cout<<"tooooooooooo"<<endl;
}
#endif

//test.cpp
#include "orderlink.h"
#include <iostream>
using namespace std;

int main()
{
  orderlist<int> or;
   
   int num=1;
   cout<<"add number int the list:"<<endl;
   for(int i=0;i<10;i++){
       or.addnum(num);
       num+=2;
   }

   cout<<"the list length is "<<or.getlength()<<endl;
   cout<<"the list is:";
  for(i=0;i<10;i++)
      cout<<or.getvalue(i)<<" ";
  cout<<endl;



  return 0;
}




补充下,这是数据结构里面的顺序表

[ 本帖最后由 coolman366 于 2009-11-14 20:33 编辑 ]
搜索更多相关主题的帖子: 模板 
2009-11-14 20:30
shl305
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:36
专家分:121
注 册:2009-3-13
收藏
得分:20 
很多错误,h文件中很多函数参数名字与函数里面用到的名字不一样,大体改了下,能运行了


程序代码:
#ifndef ORDERLIHK_H
#define ORDERLINK_H

#include <iostream>
using namespace std;
template <class T>

class orderlist
{
    public:
        orderlist();
        bool isEmpty();  //judge if the list is empty
        const int getlength();
        const int getvalue (const int);
        void addnum(const T&);
        void insert(const int ,const T ); //insert a element 
        void del(const int ); //delete a element
        void search(const T );//searth a value
        void change(const int ,const T );//change the value
    private:
//the value of element
         int length;  //the length of list
        //the position of list
        T* orlist;
};

template <class T>
orderlist<T>::orderlist()
{
    orlist= new T [100];
    length=0;
}

template <class T>
void orderlist<T>::addnum(const T &val)
{
    
    orlist[length]=val;
    length++;
}
template <class T>
bool orderlist<T>::isEmpty()
{
    return length==0;
}

template <class T>
const int orderlist<T>::getlength()
{
    return length;
}

template <class T>
const int orderlist<T>::getvalue (const int post)
{

    return orlist[post];
}
template <class T>
void orderlist<T>::insert(const int post,const T val)//insert a element
{

    if(post<=length){
      for (int i=length;i>post;i--)
            orlist[i]=orlist[i-1];
        orlist[post]=val;
        length++;
        cout<<"insert the value "<<val<<" in position "<<post<<" succefully"<<endl;
    }
    else
         cout<<post<<"is too long"<<endl;
}
template <class T>
void orderlist<T>::del(const int post) //delete a element
{
    if(post<=length){
        for(int i=post;i<length-1;i++)
             orlist[i]=orlist[i+1];
        cout<<"delete the element of position"<<post<<"succefuully"<<endl;
        length--;
    }
    else
        cout<<post<<"is to long"<<endl;
}
template <class T>
void orderlist<T>::search(const T val)//search a value
{
    for(int i=0;i<length;i++){
        if(val==orlist[i])
            cout<<"value"<<val<<"is found in position "<<i<<endl;
       else 
           cout<<"value"<<val<<"is not found in position"<<endl;
    }
}

template <class T>
void orderlist<T>::change(const int post,const T val)//change the value

{
    if(post<length){
  orlist[post]=val;
  cout<<"value "<<val<<"is sucessful changed at position"<<post<<endl;
    }
    else cout<<"tooooooooooo"<<endl;
}
#endif

#include "orderlink.h"
#include <iostream>
using namespace std;

int main()
{
  orderlist<int> orr;
   
   int num=1;
   cout<<"add number int the list:"<<endl;
   for(int i=0;i<10;i++){
       orr.addnum(num);
       num+=2;
   }

   cout<<"the list length is "<<orr.getlength()<<endl;
   cout<<"the list is:";
  for(int i=0;i<10;i++)
      cout<<orr.getvalue(i)<<" ";
  cout<<endl;



  return 0;
}
2009-11-14 21:29
coolman366
Rank: 1
等 级:新手上路
帖 子:7
专家分:5
注 册:2009-11-14
收藏
得分:0 
谢谢啦,真的可以
2009-11-14 21:55
快速回复:请教一下关于模板类的问题
数据加载中...
 
   



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

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