c++ - Template and using (#define) in class constructor -


i've implemented stack process.this program supposed work same real stack memory.moreover i'm trying use template , make the program more generic. i've got problem in using #define default_size 10 argument of class constructor.

first of when put default_size in prototype of constructor goes smoothly:

#define default_size 10  template<typename t> class stack { public:    stack(int size=default_size); private:    t *elements;    int size;    int count; };  template<typename t> stack<t>::stack(int s) {    cout << "--constructor called\n";    size = s;    elements = new t[size];    count = 0; } 

but when put default_size in outline definition of class constructor error: no appropriate default constructor available

#define default_size 10  template<typename t> class stack { public:    stack(int size); private:    t *elements;    int size;    int count; };  template<typename t> stack<t>::stack(int s=default_size) {    cout << "--constructor called\n";    size = s;    elements = new t[size];    count = 0; } 

finally main of program:

int main() {    stack<int> u;    u.push(4); } 

my question not "why can templates implemented in header file?" problem place use default_size.

i suppose, problem in difference of template declaration:

 stack(int size); 

and template definition:

stack<t>::stack(int s=default_size) {    ... } 

default values must in declaration part, , if method signature in definition different declaration (you add default_size in definition) compiler not sure write same constructor. note, default_size applied when s value not given constructor, definition work default constructor, declaration constructor 1 parameter.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -