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

0x00bbc670 {0xcdcdcdcd <读取字符串的字符时出错。>}

陈紫文 发布于 2018-09-05 17:28, 3402 次点击


#include <iostream>
using namespace std;
template<typename T>
class stack
{
public:
    explicit stack(int ss = SIZE);
    stack(const stack & a);
    bool isempty(){ return top == 0; }
    bool isfull(){ return top == stacksize; }
    bool pop(T &a);
    bool push(const T &b);
    stack& operator=(const stack &a);
    ~stack();
private:
    int    top;
    T  * ar;
    int stacksize;
    enum {SIZE=10};
};
template<typename T>
stack<T>::stack(int ss) :stacksize(ss), top(0)
{
    ar = new T [stacksize];
}
template<typename T>
stack<typename T>::~stack()
{
    delete [] ar;
}


template<typename T>
stack<typename T>::stack(const stack& a)
{
    top=a.top;
    stacksize=a.stacksize;
    ar= new T[stacksize];
    for (int i=0;i<top;i++)
    {
        ar[i]=a.ar[i];
    }
}
template<typename T>
bool stack<T>:: pop(T &a)
 {
    if (top>0)
    {
        a=ar[--top];
        cout<<"a="<<a<<endl;
        return true;
    }
    cout<<"stack is empty"<<endl;
    return false;
 }
template <typename T>
bool stack<T>::push(const T &b)
{
    if (top<stacksize)
    {
        ar[top++] = b;
        return true;
    }
    cout << "stack is full" << endl;
    return false;
   
}
template <typename T>

stack<T>& stack<T>:: operator=(const stack &a)
{
      if (this==&a)
      {
          return *this;
      }
      top=a.top;
      stacksize=a.stacksize;
      delete [] ar;
      T* ar =new [stacksize];
      for (int i=0;i<top;i++)
      {
          ar[i] = a.ar[i];
      }
      /*
      top=a.top;
      stacksize=a.stacksize;
      
      ar= new T[stacksize];
      for (int i=0;i<top;i++)
      {
          ar[i] = a.ar[i];
      }
      */

    return *this;
}

#include <iostream>
#include "array.h"
#include <ctime>
#include <cstdlib>
using namespace std;
const int num = 10;
int main()
{
   
    srand(time(0));
    const char* out[num] = { "1   :a", "2   :b", "3   :c", "4   :d", "5   :e",
        "6   :f", "7   :g", "8   :h", "9   :i", "10   :j" };
    const char * input[num];
    int process = 0;//inpoint out[];
    int innext = 0;//inpoint input[];
    int stacksize;
    cout << "input stacksize:";
   
    //int stacksize;
    cin >> stacksize;
    cout << "ok" << endl;
   

    stack<const char*> st(stacksize);
   
    while (process<num)
    {
        
        if (st.isempty())
        {
            st.push(out[process++]);
        }
        else if (st.isfull())
        {
            st.pop(input[--innext]);
        }
        else if (rand()%2 && process<num)
        {
            st.push(out[process++]);
        }
        else
        {
            st.pop(input[--innext]);
        }
        
    }

    for (int i = 0; i < num;i++)
    {
        cout << input[i] << endl;
    }
    cout << "bey" << endl;
   
    while (1);
    return 0;
}
ar    0x00bbc670 {0xcdcdcdcd <读取字符串的字符时出错。>}    const char * *
出现运行运行时错误,看我看一下
2 回复
#2
rjsp2018-09-06 09:09
完全看不懂你写的是什么,干什么用。
我只能将之改成运行不报错

            st.pop(input[--innext]); // 改为 innext++
        }
        else if (rand()%2 && process<num)
        {
            st.push(out[process++]);
        }
        else
        {
            st.pop(input[--innext]); // 改为 innext++
        }
        
    }

    for (int i = 0; i < num;i++) // 改为 innext
    {
        cout << input[i] << endl;
    }
#3
陈紫文2018-09-06 19:40
非常感谢你,我写的是c++ primer plus 上面的例子
1