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];
}
-Wall -Wextra -pedantic, it'll tell you thatT s[];is forbidden in ISO C++. You might want to use a pointer, but then, you have to follow the rule of three/five. I suggest using a smart pointer likestd::unique_ptr<T[]>instead.using namespace std;in a header file.template<class T, size_t N>, your member declared asT s[N];, and lose theNmember variable entirely. Use it asStack<double,5> var;Again, assuming fixed-at-compile-time is what you seek.std::vectororstd::list. This is C++, not C.