打印出我的链接列表会打印出其内存位置

时间:2015-04-25 01:31:37

标签: c++

好的,所以我必须读入一个存储日期和相应时间的文件date.dat

例如。)

   12/2/13
   05:30

这是我的代码:

void FillList(linkedList<myDate> &list)
{
  bool ok = true;
  int value;
  ifstream f;
  string fname;

  cout << "File: ";
  cin >> fname;
  f.open(fname.c_str());
  if (f.fail())
    {
      cout << "Failed to open" << endl;
      return;
    }

  myDate aDate;
  myTime aTime;
  f >> aDate;

  while (!f.eof())
    {
      f >> aTime;

      aDate.setTime(aTime);
      list.orderedInsert(aDate);
      cout<<aDate<<endl;
      f >> aDate;
    }

  f.close();

}

代码调用orderedInsert,它将日期和时间插入到linkedList中并按顺序排列。 OrderedInsert在这里:

template <class T>
void linkedList<T>::orderedInsert(const T &value)
{
  current=head;

  while(current!=NULL && current->data < value)
    {
      trailCurrent=current;
      current=current->next;
    }
  if(current!=NULL)
    trailCurrent->data=new node(value,current);
  else
    head->data=new node(value,current);
}

然后我要打印出我使用的列表:

void DisplayList(linkedList<myDate> list)
{
  cout << endl;
  cout << list;
}

但是当我调用DisplayList时,我得到的是每个要打印的东西的内存位置而不是日期和时间。打印输出的内存位置如下:

     ======= Memory map: ========
00400000-00405000 r-xp 00000000 00:18 5508191                            /home/STUDENTS/jskoz233/prog4/app
00605000-00606000 rw-p 00005000 00:18 5508191                            /home/STUDENTS/jskoz233/prog4/app
01fbc000-01fdd000 rw-p 00000000 00:00 0                                  [heap]
372ca00000-372ca20000 r-xp 00000000 fd:00 4718921                        /lib64/ld-2.12.so
372cc1f000-372cc20000 r--p 0001f000 fd:00 4718921                        /lib64/ld-2.12.so
372cc20000-372cc21000 rw-p 00020000 fd:00 4718921                        /lib64/ld-2.12.so

任何帮助?我完全没有头绪。谢谢!

运营商是:

template <class T>
ostream &operator<<(ostream &outStream,linkedList<T> list)
{
  T element;
  this.current=this.head;
  while(this.current!=NULL)
    {
      element=this.retrieve(element);
      outStream<<"["<<element<<"]"<<endl;
      this.current++;
    }
  return outStream;
}

复制构造函数:

//Copy Constructor
template <class T>
linkedList<T>::linkedList(const linkedList &list)
{
  node *hurrah;
   hurrah=list.head;
delete this;
while(hurrah!=NULL)
  {
      orderedInsert(hurrah->data);
      hurrah=hurrah->next;
  }    
}

retriev:

//retrieve
template <class T>
bool linkedList<T>::retrieve(T &value) const
{
  if(current==NULL)
    return false;
  else
    {
      value=current->data;
      return true;
    }

}

0 个答案:

没有答案