C ++:显示动态数组类

时间:2016-11-21 21:27:38

标签: c++ cout dynamic-arrays

我正在尝试在类中打印数组的所有元素,但不知道从哪里开始。

尝试1:Cout不起作用,因为这是一个类。没有cout的定义。 尝试2:重载ostream<<运算符似乎不会起作用,因为我没有索引来简单地为每个元素创建一个for循环。 尝试3:有没有办法定义一个类似于使用带索引的数组的复制构造函数的新函数?

我是C ++的初学者,所以我很感激任何帮助&解释。

// stack.h -- class definition for the stack array
#include    <stdlib.h>
#include    <iostream>
using namespace std;

class DoubleStack
{
private:
    // variables: 
    double  *data;      // pointer to the actual array of data 
    size_t  stack_size; // Capacity of stack
    size_t  tos;        // Top of stack. tos==0 ==> empty stack

public:
    DoubleStack(size_t capacity);           // Constructor
    DoubleStack(const DoubleStack& rhs);    // Copy Constructor
    ~DoubleStack(void);                     // Destructor
    DoubleStack& operator=(DoubleStack& rhs); // Assignment operator 
    friend ostream& operator << (ostream& os, const DoubleStack& dbl); //Stream insertion operator for Doublestack
};

.cpp定义

    #include "dblstk.h"
DoubleStack::DoubleStack(size_t capacity)       // create an empty stack
{
    data = new double[capacity];                
    stack_size = capacity;                      
    tos = 0;                                    
}

DoubleStack::DoubleStack(const DoubleStack& rhs)// copy constructor
{
    data = new double[rhs.stack_size];          
    stack_size = rhs.stack_size;                
    tos = rhs.tos;
    for (int i = 0; i < stack_size; i++)        
    {
        data[i] = rhs.data[i];                  
    };  
}

DoubleStack::~DoubleStack(void)                 // Destructor
{
    delete[] data;
}

DoubleStack& DoubleStack::operator=(DoubleStack& rhs) // Assignment operator
{                                                   
    delete[] data;
    data = new double[rhs.stack_size];
    stack_size = rhs.stack_size;
    tos = rhs.tos;
    for (int i = 0; i < stack_size; i++)        
    {
        data[i] = rhs.data[i];
    };
    return *this;                               
}

ostream& operator << (ostream& os, const DoubleStack& dbl) 
{
    for (int i = 0; i<capacity; i++)
    {
         os << dbl[i];
    } // THIS IS WRONG
    return os;
}

1 个答案:

答案 0 :(得分:1)

您的循环应全部使用tos作为限制,而不是stack_sizecapacity

<<功能中,您需要访问dbl.tosdbl.data[i](除非您将重载[]运算符添加到DoubleStack)。我在堆栈的元素之间添加了一个逗号分隔符。

#include "dblstk.h"
DoubleStack::DoubleStack(size_t capacity)       // create an empty stack
{
    data = new double[capacity];                
    stack_size = capacity;                      
    tos = 0;                                    
}

DoubleStack::DoubleStack(const DoubleStack& rhs)// copy constructor
{
    data = new double[rhs.stack_size];          
    stack_size = rhs.stack_size;                
    tos = rhs.tos;
    for (int i = 0; i < tos; i++)        
    {
        data[i] = rhs.data[i];                  
    };  
}

DoubleStack::~DoubleStack(void)                 // Destructor
{
    delete[] data;
}

DoubleStack& DoubleStack::operator=(DoubleStack& rhs) // Assignment operator
{                                                   
    delete[] data;
    data = new double[rhs.stack_size];
    stack_size = rhs.stack_size;
    tos = rhs.tos;
    for (int i = 0; i < tos; i++)        
    {
        data[i] = rhs.data[i];
    };
    return *this;                               
}

ostream& operator << (ostream& os, const DoubleStack& dbl) 
{
    for (int i = 0; i<dbl.tos; i++)
    {
        if (i > 0) {
            os << ",";
        }
        os << dbl.data[i];
    }
    return os;
}