c++ - How to initialize member variable array with template type? -
how can initialize array, s of template type t in constructor stack()? might simple question, don't know of c++. error when compiling (gnu gcc):
error: incompatible types in assignment of 'double*' 'double [0]'
this how i'm initializing stack object in main.cpp:
stack<double> stack; and here stack.h file (the implementation 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]; }
change
//... private: t s[]; to
//... private: t *s; this declaration
private: t s[]; is not c++ compliant. size of array shall constant expression. declared array use pointer in constructor
s = new t[5]; or use std::vector instead of manually allocated array.
Comments
Post a Comment