Domanda

I have a "stack.h" and a "stack.cpp" file that define a hand-made stack class.

What i want to do now, is to create another class "name" that has in it's composition a vector of "nr" stacks and i'm not sure where to start.

depou.h:

#pragma once
#include <vector>
#include "stiva.h"

template <class T>
class depou {
private:
    int nr;
    std::vector<stiva<T> > depouArray;

public:
    depou (int x, int lungTren);
};

template <class T>
depou<T>::depou (int x, int lungTren){
    int i;
    nr = x;
    depouArray = new std::vector<stiva<T> > (nr);
/*  for (i=0; i<nr; i++){
        depouArray[i] = stiva<T> (lungTren);
    } this is incorrect */
}

My name.h doesn't compile i just want to know if it's possible to do a vector of that kind.

My stack header is called "stiva", so i edited the "name.h" file according so.

stiva.h

#pragma once

template <class T>
class stiva {
private:
    int top;
    int size;
    T *stackArray;

public:
    stiva();
    stiva (int s);
    ~stiva (){ delete [] stackArray; }
    int push (const T&);
    int pop (T&);
  int topElem (T&);
    int isEmpty (){return top == -1;}
    int isFull () {return top == size -1;}
    int search (T x);
    void setSize (const int& sz){
        size=sz;
    }
};

template <class T>
stiva<T>::stiva (){
    size = 10;
    top = -1;
    stackArray = new T[size];
}

template <class T>
stiva<T>::stiva (int s){
    size = s>0 && s<1000? s : 10;
    top = -1;
    stackArray = new T[size];
}

template <class T>
int stiva<T>::push (const T& x){
    if (!isFull()){
        stackArray[++top] = x;
        return 1;
    }
    else{
        size *= 2;
        stackArray = (T*) realloc (stackArray, 2*size * sizeof(T));
        stackArray[++top] = x;
        return 1;
    }
    return 0;
}

template <class T>
int stiva<T>::pop (T& popValue){
    if (!isEmpty ()){
        popValue = stackArray[top--];
        return 1;
    }
    return 0;
}

template <class T>
int stiva<T>::topElem (T& topValue){
    if (!isEmpty ()){
        topValue = stackArray[top];
        return 1;
    }
    return 0;
}

in main i initialize like this:

depou d(5,10);

È stato utile?

Soluzione 2

You can initialize the vector to your given size in the member intializer of the constructor:

template <class T>
class name {
private:
    int nr;
    std::vector<stack<T> > nameArray;

public:
    name (int x) : nr(x), nameArray(nr) {}
    //other methods
};

Working example here: http://codepad.org/ZT9iuQEg

Altri suggerimenti

You don't need to use calloc in that code (and if that compiles, it's probably malicious as well).

In fact, if your code just need to rename a type like std::vector<std::stack<T>> to something more readable, you can use:

template<typename T> using name = std::vector<stack<T>>;

instead.

Otherwise you can just fix your current code with:

name (int x)
    : nr(x)
    , nameArray(nr)
    {}

The declaration of nameArray is ok as long as your stack accepts a template parameter. But you are using nameArray quite wrongly. Try this:

name (int x){
  nr = x;
  nameArray.resize(x);
}

In the constructor, you have:

    nameArray = (stack<T>*) calloc (nr, sizeof(stack<T>) );

There are couple of problems with this line.

  1. In this statement, you are assigning a stack<T>* to a std::vector>. The compiler should tell you that is not a valid assignment.
  2. Try to use the new and delete operators in C++.

You can fix the problems by changing the constructor to a much simpler form:

name (int x) : nr(x), nameArray(x) {}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top