注册 登录
编程论坛 C++教室

请教一下关于模板类的问题

coolman366 发布于 2009-11-14 20:30, 422 次点击
语法没出错,就是运行得不到结果,想了半天没找出错误,敢情哪位大虾指点下,不胜感激,本人在线:
代码如下:
//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 编辑 ]
2 回复
#2
shl3052009-11-14 21:29
很多错误,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;
}
#3
coolman3662009-11-14 21:55
谢谢啦,真的可以
1