内存地址而不是cout类中的值

时间:2016-12-07 10:12:13

标签: c++ smart-pointers forward-list

我正在尝试编写自己的简单转发列表实现。我想通过cout<<访问元素列表[0]。我写了下面的代码,但没有值,我有像x637c00539997。我做错了什么? 我的代码还能改进什么?

#include <iostream>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <memory>

using namespace std;

class myList{
public:

  class myListExceptionEmpty: public exception
  {
  public:
    virtual const char* what() const throw()
    {
      return "EMPTY";
    }
  };

  void push_back(int valve);
  int getSize();
  bool isEmpty();
  void removeFirst();
  void remove(int x);
  void dump();
  void pop_front();
  struct elem
  {
    std::shared_ptr<elem> next;
    int val;
  };
  class proxy
  {
  public:
    std::shared_ptr<myList::elem>  position;
    proxy(std::shared_ptr<myList::elem> pos)
    {
      position = pos;
    }
  };
  std::shared_ptr<myList::proxy> operator[](int position);

private:
  std::shared_ptr<elem> start;
  std::shared_ptr<elem> getLastElement();
  std::shared_ptr<proxy> current;
  int size = 0;


};

ostream& operator<<(std::ostream& os, myList::proxy& obj)
{
      os << obj.position->val;
      return os;
}

shared_ptr<myList::proxy> myList::operator[](int position)
{
  std::shared_ptr<elem> p = start;
  for(int i=0;i<position;i++)
  {
    p = p->next;
    if (p == NULL) throw std::out_of_range("out");
  }
  //cout << p->val;
  std::shared_ptr<proxy> tmp(new myList::proxy(p));
  current = tmp;
  return current;
}

std::shared_ptr<myList::elem> myList::getLastElement()
{
    std::shared_ptr<elem> p = start;
    while(p->next != NULL ) p = p->next;
    return p;
}

bool myList::isEmpty()
{
  return size;
}

void myList::dump()
{
  std::shared_ptr<elem> x = start;
  while (x != NULL)
  {
      cout << x->val << endl;
        x = x->next;
  }
}

void myList::push_back(int valve)
{
  std::shared_ptr<elem> p, n(new elem());
  n->next = NULL;
  n->val = valve;
  p = start;
  if(p != NULL)
  {
     while(p->next != NULL ) p = p->next;
     p->next = n;
  }
  else start = n;
  size++;

}

void myList::remove(int x)
{
  if (size == 0) throw myListExceptionEmpty();
  std::shared_ptr<elem> p, prv;
  p = start;
  while(p->next != NULL)
  {
    if (p->val == x)
    {
      prv->next = p->next;
      size--;
    }
    prv = p;
    p = p->next;
  }
}

void myList::pop_front()
{

  if (size == 0) throw myListExceptionEmpty();
  std::shared_ptr<elem> p, prv;
  p = start;
  while(p->next != NULL)
  {
    prv = p;
    p = p->next;
  }
  prv->next = NULL;

}
void myList::removeFirst()
{
  if (size == 0) throw myListExceptionEmpty();
  std::shared_ptr<elem> p;
  p = start;
  cout << start->val << endl;
  if(p!= NULL)
  {
    start = p->next;
  }
  size--;

}

int myList::getSize()
{
  return size;
}

int main()
{
  myList array;
  int size;
  cin >> size;

  char a;
  int tmp;
  for (int i=0; i<size; ++i) {
    std::cin >> a;
    if (a == 'D')
    {
      try{
      array.removeFirst();
    }
    catch (myList::myListExceptionEmpty &e)
    {
      cout << e.what() << endl;
    }
    }
    else if (a == 'A')
    {
        int tmp;
        std::cin >> tmp;
        array.push_back(tmp);
        cout << "elem" << array[0];
    }
    else if (a == 'S') cout /*<< "Size:"*/ << array.getSize() << endl;
  }

}

2 个答案:

答案 0 :(得分:1)

关于问题&#34;还有什么需要改进&#34;:

  • 您正在使用共享指针,太棒了!而不是使用new使用相应的例程make_shared(参见C ++文档)。
  • 不要在类中定义类。让每个类都有自己的头文件和源文件。它可以显着提高可读性,甚至可以通过将所有类拆分为不同的文件来加快编译速度(最好的方法:使用包含警卫)。这样您也可以保留myList::elem
  • 这样的名称空间
  • 使用<cassert>代替<assert.h>。你还在使用C ++ 11,为什么需要使用旧的C库?!
  • 只是一个示例:将void myList::remove(int x)更改为void myList::remove(const int& x)。让编译器知道x是一个只读对象。不要在这里调用int-copy构造函数(也在其他代码行中调用)。

关于你的第二个问题:你的重载运算符[]返回一个共享指针,而不是这个指针指向的对象。因此,它将打印指针的地址。

答案 1 :(得分:0)

这是修复后的程序。我用cout&lt;&lt; *阵列[0];而不是cout&lt;&lt;阵列[0] ;.比你帮忙

#include <iostream>
#include <memory>

using namespace std;
class MyListProxy;
class MyListExceptionEmpty: public exception
{
public:
  virtual const char* what() const throw(){
    return "EMPTY";
  }
};



class MyList{
public:
  void push_back(int valve);
  int getSize() { return size; };
  bool empty() { return size; };
  void removeFirst();
  void remove(const int& x);
  void dump();
  void pop_front();
  void clear() {this->start = NULL; size = 0;}
  struct elem
  {
    shared_ptr<elem> next;
    int val;
  };
  shared_ptr<MyListProxy> operator[](int position);
private:
  shared_ptr<elem> start;
  shared_ptr<MyListProxy> current;
  shared_ptr<elem> getEnd();
  int size = 0;

};

class MyListProxy
{
public:
  shared_ptr<MyList::elem> position;
  MyListProxy(shared_ptr<MyList::elem> pos)
  {
    position = pos;
  }
};

ostream& operator<<(ostream& os, MyListProxy& obj)
{
  os << obj.position->val;
  return os;
}

shared_ptr<MyListProxy> MyList::operator[](int position)
{
  if (start == NULL) throw out_of_range("out");
  shared_ptr<elem> p = start;
  for(int i=0;i<position;i++){
    p = p->next;
    if (p == NULL) throw out_of_range("out");
  }
  return make_shared <MyListProxy>(p);
}

shared_ptr<MyList::elem> MyList::getEnd()
{
  if (start == NULL) return start;
   shared_ptr<elem> p = start;
   while(p->next != NULL ) p = p->next;
   return p->next;
}

void MyList::dump()
{
  shared_ptr<elem> x = start;
  while (x != NULL)
  {
    cout << x->val << endl;
    x = x->next;
  }
}

void MyList::push_back(int valve)
{
  shared_ptr<elem> p;
  auto last = this->getEnd();
  auto a =  make_shared<elem>();
  last = a;
  last->val = valve;
  size++;

}

void MyList::remove(const int& x)
{
  if (size == 0) throw MyListExceptionEmpty();
  shared_ptr<elem> p, prv;
  p = start;
  while(p->next != NULL)
  {
    if (p->val == x)
    {
      prv->next = p->next;
      size--;
    }
    prv = p;
    p = p->next;
  }
}

void MyList::pop_front()
{

  if (size == 0) throw MyListExceptionEmpty();
  start = start->next;
}


int main()
{
  MyList array;
  int size;
  cin >> size;

  char a;
  int tmp;
  for (int i=0; i<size; ++i) {
    cin >> a;
    if (a == 'D')
    {
      try{
        cout << *array[0];
        array.pop_front();
      }
      catch (MyListExceptionEmpty &e)
      {
        cout << "EMPTY" << endl;
      }
      catch (out_of_range &e)
      {
        cout << "EMPTY" << endl;
      }
    }
    else if (a == 'A')
    {
      int tmp;
      cin >> tmp;
      array.push_back(tmp);
    }
    else if (a == 'S') cout /*<< "Size:"*/ << array.getSize() << endl;
    cout << "SIZE::" << array.getSize() << endl;
  }

}