0

How can I initialize an array, s of template type T in my constructor Stack()? This might be a really simple question, but I don't know much of c++. I get this error when compiling (GNU GCC):

error: incompatible types in assignment of 'double*' to 'double [0]'

This is how I'm initializing the Stack object in my main.cpp:

Stack<double> stack;

And here is my Stack.h file (the implementation is included):

#pragma once
#include <iostream>

using namespace std;

template <class T>

class Stack
{
    public:
        Stack();

    private:
        T s[];
        int N;
};

template <class T>

Stack<T>::Stack() {
    s = new T[5];
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Daniel Node.js
  • 6,734
  • 9
  • 35
  • 57
  • 1
    You should compile with `-Wall -Wextra -pedantic`, it'll tell you that `T s[];` is forbidden in ISO C++. You might want to use a pointer, but then, you have to follow the [rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)/[five](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11). I suggest using a smart pointer like `std::unique_ptr` instead. – dyp Mar 10 '14 at 20:46
  • Also, you shouldn't use `using namespace std;` in a header file. – dyp Mar 10 '14 at 20:48
  • Is this "stack" supposed to be fixed size? If so, use `template` , your member declared as `T s[N];`, and lose the `N` member variable entirely. Use it as `Stack var;` Again, assuming fixed-at-compile-time is what you seek. – WhozCraig Mar 10 '14 at 20:51
  • You should really use a standard template library, such as `std::vector` or `std::list`. This is C++, not C. – djhaskin987 Mar 10 '14 at 21:27

3 Answers3

2

Change

//...
private:
    T s[];

to

//...
private:
    T *s;

This declaration

private:
    T s[];

is not C++ compliant. The size of the array shall be a constant expression. Moreover you declared an array but use it as a pointer in the constructor

s = new T[5];

Or use std::vector instead of the manually allocated array.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • +1 That comes nearest to the originally given code. Though I think the most important mention in your answer is recommending usage of `std::vector`. – πάντα ῥεῖ Mar 10 '14 at 21:01
1

First, the size of array put on stack has to be known at compile time, so

T s[];

shoud read for instance

T s[10];

Second array of T is different type then T* return by new.

Solution:

class Stack
{
    public:
        Stack();
        ~Stack() { delete[] s_;}  // to avoid memory leak delete the array

    private:
        T* s;
        int N;
};

template <class T>
Stack<T>::Stack() {
    s = new T[10];
}

usage:

int main() {
    Stack<int> s;
    return 0;
}

Even better, make the size of array a template parameter:

template <class T, size_t N>
class Stack
{
    public:
        Stack();
        ~Stack() { delete[] s_;}  // to avoid memory leak delete the array

    private:
        T* s_;
        int N_;
};

template <class T, size_t N>
Stack<T,N>::Stack() : N_( N) {
    s_ = new T[N_];
}

usage:

int main() {
    Stack< int,10> s;
    return 0;
}

Even better

Use templates, make standard container a template parameter:

template <class T, size_t N, template<class T, 
                                           class = std::allocator<T> > class C >
class Stack
{
    public:
        Stack();
        ~Stack() {}

    private:
        C<T> s_;
        int N_;
};

template <class T, size_t N, template<class T, 
                                           class = std::allocator<T> > class C >
Stack<T,N,C>::Stack() : N_( N) {
    s_.resize( N_);
}

usage:

int main() {
    Stack< int,10,std::vector > s;
    return 0;
}
4pie0
  • 29,204
  • 9
  • 82
  • 118
1

One alternative is to give the array size as what is called a non-type template parameter (I'm assuming N is meant to represent the current stack size):

template <class T, int size>
class Stack
{
    public:
        Stack() : N(0) {}

    private:
        T s[size];
        int N;
};

You could use it as follows:

Stack<double, 5> myStack;
Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55